Given this code:
function Test(){
var $D = Date.prototype;
$D.addDays=function(value){this.setDate(this.getDate()+value);return this;};
$D.clone = function() {
return new Date(this.getTime());
};
alert(new Date().addDays(5));
var x = 5;
}
Test();
alert(new Date().addDays(1));
alert(x);
The only error is thrown by alert(x)
which throws the "x is undefined" error, because x is limited to function Test()
The call to addDays()
works both within the function and outside it.
I'm assuming this is because the scope of the Date prototype is global, so it's irrelevant that I extend it within a closure.
So if I wanted to extend Date within a closure, BUT NOT allow any other javascript to get the extension, how would I do it?
The reason I'm asking is because when I'm extending core javascript objects, I don't want to conflict with other libraries which may make the same modifications.