Changing JavaScript's global object?
<p>Is there a way to change the <em>root</em> object in JavaScript?</p>

<p>For example, in browsers, the <em>root</em> object is "window". So </p>

<pre><code>X = 5;
console.log(Y);
</code></pre>

<p>is the same as:</p>

<pre><code>window.X = 5;
console.log(window.Y);
</code></pre>

<p>What I want to do now, is changing this <em>root</em> object, so when I do the following:</p>

<pre><code>X = 6;
</code></pre>

<p><strong>Reason why I need this:</strong></p>

<p>In <a href="http://en.wikipedia.org/wiki/Node.js" rel="nofollow">Node.js</a> applications, every part of the program can access the global object. That's a big problem because every script that is executed by a Node.js webserver can add new variables to it. They will be there until the webserver is restarted. I want to avoid this by changing the global object.</p>

<p><strong>Update</strong></p>

<p>I've tested the following code and got a really interesting result.
What did you expect of the following code?</p>

<p>var X = {A:"a",B:"b"};</p>

<pre><code>with(X){
    A = 5;
    C = 7;
}
for(a in X){
    console.log(a+" is "+X[a]);
}

/* 
Expected Console Output:
A is 5
B is b
C is 7

Real Console Output:
A is 5;
B is b;

*/
</code></pre>

<p>Is there a way to get output as I expected it?</p>

<p><strong>Update</strong></p>

<p>I've now tested the module system with the following code.</p>

<pre><code>//program.js
var t = require("./module.js");
t.Test();

console.log(A);

//module.js
A = 5;
exports.Test = function(){
    console.log("hello world!");
}
</code></pre>

<p>The output was:</p>

<pre><code>hello world!
5
</code></pre>

<p>This tells me, that the variable "A" defined in <code>module.js</code> was added to the global object of <code>program.js</code>. The module does not solve my problem, either.</p>
