Some important distinctions first (you may already know this, but just so we're on the same page).
prototype
changes the behavior of all objects (instances and future instances). So tread lightly when modifying built in types (Date, String, etc.) as you'll be changing the behavior of these objects for every routine that is using them.
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
To answer your specific questions:
- It looks like you are overridding the native function correctly. Storing a copy of the native function so you can call it like a
base
function.
- From my example above, you'll either need to modify all Date instances or just the one in question.
var date = new Date(); date.done = false;
Date.prototype.done = false;
- The reason
this
doesn't work in your outer function is because it is a self executing anonymous function and is executed immediately in order to assign one of your inner functions to the getFullYear
method.
All in all I'm not sure your approach is the best idea due to the danger of interfering with other libraries, and I'm not sure why you'd only want this function to return +150 a single time per Date instance, but here is your code modified to perform your intended logic.
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());