Specify scope for eval() in JavaScript?
eval, javascript, scope
Solution
you can use the "use strict" to contain the eval'ed code within the eval itself.
Second, `eval` of strict mode code does not introduce new variables into the surrounding scope. In normal code `eval("var x;")` introduces a variable `x` into the surrounding function or the global scope. This means that, in general, in a function containing a call to `eval` every name not referring to an argument or local variable must be mapped to a particular definition at runtime (because that `eval` might have introduced a new variable that would hide the outer variable). In strict mode `eval` creates variables only for the code being evaluated, so eval can't affect whether a name refers to an outer variable or some local variable
var x = 17; //a local variable
var evalX = eval("'use strict'; var x = 42; x"); //eval an x internally
assert(x === 17); //x is still 17 here
assert(evalX === 42); //evalX takes 42 from eval'ed x
If a function is declared with "use strict", everything in it will be executed in strict mode. the following will do the same as above:
function foo(){
"use strict";
var x = 17;
var evalX = eval("var x = 42; x");
assert(x === 17);
assert(evalX === 42);
}
Problem
is there any way I can execute eval() on a specific scope (but NOT global)? for example, the following code doesn't work (a is undefined on the second statement) because they are on different scope: ``` eval(var a = 1); eval(alert(a)); ``` 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) ``` 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 } ``` Any idea on how to achieve something like this? Thanks!