首先是一些重要的区别(您可能已经知道这一点,但我们在同一页面上)。
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
要回答您的具体问题:
- 看起来您正确地覆盖了本机功能。存储本机函数的副本,以便您可以像调用
base
函数一样调用它。
- 在我上面的示例中,您要么需要修改所有 Date 实例,要么只修改一个有问题的实例。
var date = new Date(); date.done = false;
Date.prototype.done = false;
- 原因
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());