title: JavaScript - How to hide global scope from eval'd script
body: <p>I usually think of global scope as a namespace that always can be accessed from everywhere. I would like to know whether it is theoretically possible to <em>completely hide</em> global scope. For example, assume we have some code we would like to <code>eval</code>uate (in the console of a browser):</p>
  
  <pre><code>var code = 
    "console.log(this);   " + // access the global object directly
    "console.log(window); " + // access the global object as the window object
    "newGlobalVar = 42;   ";  // implicitly create global object
  eval(code);
  </code></pre>
  
  <p>By wrapping the <code>eval</code> call, <code>this</code> and <code>window</code> can be hidden from <code>code</code>:</p>
  
  <pre><code>(function (window) { eval(code); }).call({});
  </code></pre>
  
  <p>But I can't stop the <code>code</code> implicitly create global variables. Is it possible somehow? I don't want to use this stuff, I'm just curious.</p>