Is there a way to specify which object to use for global when invoking eval()?
(I’m not asking how to do global eval().)
This is not working but this illustrates what I would like:
var pseudoGlobal = {};
eval("x = 12", pseudoGlobal);
pseudoGlobal.x; // 12
The point is that real global bindings are not affected by implicit variable declaration (i.e. without var keywords) in the code eval()’ed.
As for eval.call(pseudoGlobal, "x=12") or eval.apply(pseudoGlobal, ["x=12"]), some interpreters wont allow it.
You can, of course, substitute default object for assigning a property value, like in
but not for creating a propery. If a property is not found in the current stack of execution contexts, it’s created in the global object. That’s all there is to it.
You might try some weird things, also:
If you care about global bindings, try:
this way the bindings will be changed in globalvars. Note, that shallow copy will prevent only one level of bingings to change.