how to override dojo function

时间:2015-06-26 09:38:03

标签: javascript dojo

I'd like to correct a problem of incorrect week number in dojox/calendar/Calendar. I know what to change => exports._getWeekOfYear function in dojo/date/locale

it works perfectly if I put it in the js file, but, I do not want/cannot modify dojo's files.

I wanted to apply overriding solutions found here:

http://g00glen00b.be/dojo-inheritance-overriding-extending/ or here

without any success I get a SyntaxError: missing : after property id

on the line with var obj = new exports();

code from my last try:

require(["dojo/_base/lang", "dojo/date/locale"], function(lang, locale){
    lang.extend(locale, {
        var obj = new exports();
        obj._getWeekOfYear = function(/*Date*/ dateObject, /*Number*/ firstDayOfWeek){
            if(arguments.length == 1){ firstDayOfWeek = 0; }
            var determinedate = new Date();
            determinedate.setFullYear(dateObject.getFullYear(), dateObject.getMonth(), dateObject.getDate());
            var D = determinedate.getDay();
            if(D == firstDayOfWeek) D = 7;
            determinedate.setDate(determinedate.getDate() + (4 - D));
            var YN = determinedate.getFullYear();
            var ZBDoCY = Math.floor((determinedate.getTime() - new Date(YN, 0, 1, -6)) / 86400000);
            var WN = 1 + Math.floor(ZBDoCY / 7);
            return WN;
        };
    });
});

i tried :

exports._getWeekOfYear: function(...)    
exports._getWeekOfYear=: function(...)

Any ideas?

2 个答案:

答案 0 :(得分:2)

在没有提供整个解决方案的情况下,我注意到的第一件事是"出口"不是可以使用new关键字实例化的类。在该特定的dojo模块中,exports只是一个公开API的对象。

查看SitePen上的解决方案,特别是" MonkeyPatching"是一种对我有用的机制。

答案 1 :(得分:1)

感谢

我终于设法用这段代码做了我想要的事情

require(["dojo/date/locale"], function( local){

        local._getWeekOfYear= function(/*Date*/ dateObject, /*Number*/ firstDayOfWeek){
            if(arguments.length == 1){ firstDayOfWeek = 0; }
            var determinedate = new Date();
            determinedate.setFullYear(dateObject.getFullYear(), dateObject.getMonth(), dateObject.getDate());
            var D = determinedate.getDay();
            if(D == firstDayOfWeek) D = 7;
            determinedate.setDate(determinedate.getDate() + (4 - D));
            var YN = determinedate.getFullYear();
            var ZBDoCY = Math.floor((determinedate.getTime() - new Date(YN, 0, 1, -6)) / 86400000);
            var WN = 1 + Math.floor(ZBDoCY / 7);
            return WN;
        }
    ;
});

确实是出口是pb。