title: Specify scope for eval() in JavaScript?
body: <p>is there any way I can execute eval() on a specific scope <strong>(but NOT global)</strong>?</p>
  
  <p>for example, the following code doesn't work (a is undefined on the second statement) because they are on different scope:</p>
  
  <pre><code>eval(var a = 1); 
  eval(alert(a));
  </code></pre>
  
  <p>If possible, I would like to create a scope on the fly. for example (the syntax is definitely wrong, but just to illustrate the idea)</p>
  
  <pre><code>var scope1;
  var scope2;
  with scope1{
      eval(var a = 1); eval(alert(a));  // this will alert 1
  }
  with scope2{
      eval(var a = 1); eval(a++); eval(alert(a));  // this will alert 2
  }
  with scope1{
      eval(a += 2); eval(alert(a)); // this will alert 3 because a is already defined in scope1
  }
  </code></pre>
  
  <p>Any idea on how to achieve something like this? Thanks!</p>