Javascript prototype and closures
<p>Given this code:</p>

<pre><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);​
</code></pre>

<p>The only error is thrown by <code>alert(x)</code> which throws the "x is undefined" error, because x is limited to function <code>Test()</code></p>

<p>The call to <code>addDays()</code> works both within the function and outside it.</p>

<p>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.</p>

<p>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?</p>

<p>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.</p>
