2

jQuery Datepicker 小部件基于内置的 Date。我正在尝试覆盖 Date 来计算我们自己的具有不同年份、月份 .... 的日历(无需更改 datepicker 代码)。我尝试了下面的代码进行测试,但是这个覆盖被调用了两次。

Date.prototype.getFullYear = (function(){
    var $gfy = Date.prototype.getFullYear;
    if (!this.done) {
        this.done = true; // add my own variable to this instance
        return function() {
             return $gfy.call(this)+150;
       };
    } else {
        return function() {
             return $gfy.call(this);
        };
    }
}());

所以,

  1. 什么是覆盖本机功能的正确方法和

  2. 如何将变量添加到原始实例。

  3. 如何获取实例本身(这在这里不起作用)。

4

1 回答 1

1

首先是一些重要的区别(您可能已经知道这一点,但我们在同一页面上)。

prototype改变所有对象(实例和未来实例)的行为。因此,在修改内置类型(日期、字符串等)时要小心,因为您将为使用它们的每个例程更改这些对象的行为。

Live Demo

//Add a property to the instance
var date = new Date(); 
date.newProp = 'test'; 
alert(date.newProp); //test

//----VS----

//Add a property to all instances existing 
//and too be created
Date.prototype.newProp2 = 'new prop'; 

var date2 = new Date(); 
alert(date2.newProp);//undefined 
alert(date2.newProp2); //new prop

//See how the prototype modified the existing instance as well. 
alert(date.newProp2); //new  prop

要回答您的具体问题:

  1. 看起来您正确地覆盖了本机功能。存储本机函数的副本,以便您可以像调用base函数一样调用它。
  2. 在我上面的示例中,您要么需要修改所有 Date 实例,要么只修改一个有问题的实例。
    • var date = new Date(); date.done = false;
    • Date.prototype.done = false;
  3. 原因this在您的外部函数中不起作用是因为它是一个自我执行的匿名函数,并且会立即执行以便将您的内部函数之一分配给该getFullYear方法。

总而言之,由于存在干扰其他库的危险,我不确定您的方法是否是最好的主意,而且我不确定您为什么只希望此函数在每个 Date 实例中一次返回 +150,但是这是您修改后的代码以执行您的预期逻辑。

Live Demo

//Add a variable to all Date objects
Date.prototype.done = false; 

//Override the getFullYear function
Date.prototype.getFullYear = (function(){

    //Store a reference to the native function
    var $gfy = Date.prototype.getFullYear;
    return function(){

        //Have we hit this routine before?
        if (!this.done) {

            //We've hit this routine once so flag it.
            this.done = true;

            //Execute native funciton, then add 150
            return $gfy.call(this)+150;
        } else {

            //Call the native function from now on.
            return $gfy.call(this);
        }
    }
}());//self executing function so it executes as soon as it is evaluated.

var date = new Date(); 

//Will return +150 the first time
alert(date.getFullYear()); 

//After that execute the default 
//function for'getFullYear'
alert(date.getFullYear()); 
于 2013-04-14T17:39:27.850 回答