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
with (pseudoGlobal) eval("x=12")
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:
//global code
var globalvars = {};
for (i in this)
globalvars[i] = null;
with (pseudoGlobal)
eval("x=12")
for (i in this)
if (!(i in globalvars))
{
pseudoGlobal[i] = this[i];
delete this[i];
}
If you care about global bindings, try:
var globalvars = {};
for (i in this)
globalvars[i] = this[i];
with (globalvars)
eval("x=12")
this way the bindings will be changed in globalvars. Note, that shallow copy will prevent only one level of bingings to change.
There is no built-in way to do this.
There are two solutions that come to mind:
eval
ed code. i.e., instead of x = 12
, you would have to do something like o.x = 12
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With