Javascript扩展函数返回的内容?

时间:2015-03-20 11:35:08

标签: javascript python override

有这个功能(用更多代码更新):

openerp.web_calendar = function(instance) {
    var _t = instance.web._t,
        _lt = instance.web._lt,
        QWeb = instance.web.qweb;

    function get_fc_defaultOptions() {
        shortTimeformat = Date.CultureInfo.formatPatterns.shortTime;
        var dateFormat = Date.normalizeFormat(instance.web.strip_raw_chars(_t.database.parameters.date_format));
        return {
            weekNumberTitle: _t("W"),
            allDayText: _t("All day"),
            buttonText : {
                today:    _t("Today"),
                month:    _t("Month"),
                week:     _t("Week"),
                day:      _t("Day")
            },
            monthNames: Date.CultureInfo.monthNames,
            monthNamesShort: Date.CultureInfo.abbreviatedMonthNames,
            dayNames: Date.CultureInfo.dayNames,
            dayNamesShort: Date.CultureInfo.abbreviatedDayNames,
            firstDay: Date.CultureInfo.firstDayOfWeek,
            weekNumbers: true,
            axisFormat : shortTimeformat.replace(/:mm/,'(:mm)'),
            timeFormat : {
               // for agendaWeek and agendaDay               
               agenda: shortTimeformat + '{ - ' + shortTimeformat + '}', // 5:00 - 6:30
                // for all other views
                '': shortTimeformat.replace(/:mm/,'(:mm)'),  // 7pm
            },
            titleFormat: {
                month: 'MMMM yyyy',
                week: dateFormat + "{ '—'"+ dateFormat,
                day: dateFormat,
            },
            columnFormat: {
                month: 'ddd',
                week: 'ddd ' + dateFormat,
                day: 'dddd ' + dateFormat,
            },
            weekMode : 'liquid',
            aspectRatio: 1.8,
            snapMinutes: 15,
        };
    }
};

我想扩展返回的内容。确切地说,我需要在month: 'h:mm{ - h:mm}'对象中timeFormat

在Python中我可以做这样的事情(如果return是字典):

def some_class(object):
    def get_fc_defaultOptions(self):
        ret = super(some_class, self).get_fc_defaultOptions()
        ret['timeFormat']['month'] = 'h:mm{ - h:mm}'
        return ret

JavaScript中的等价物是什么?

2 个答案:

答案 0 :(得分:1)

您只需指定它返回的对象的属性:

var obj = get_fc_defaultOptions();
obj.timeFormat.month = 'h:mm{ - h:mm}';

或者,如果你想要一个功能,你可以打电话来做:

function some_class() {
    var obj = get_fc_defaultOptions();
    obj.timeFormat.month = 'h:mm{ - h:mm}';
    return obj;
}

用法:

var obj = some_class();
console.log(obj.timeFormat.month); // 'h:mm{ - h:mm}'

(我不会称之为some_class。)

答案 1 :(得分:1)

你想要一个有get_fc_options的新类吗?

好吧,看到get_fc_defaultOptions是一个基本功能,你只需要上课

function YourClass() {
    this.get_fc_defaultOptions = function () {
        var ret = get_fc_defaultOptions(); // This calls the global function
        // Your code here
        return ret;
    }
}

var instance = new YourClass();

我希望我能正确理解你的问题,在26小时醒来之后真的不应该回答问题。