In the scenario below, how can I get references to the variables declared during eval() if I do not know their names?
function test() {
eval("var myVariable = 5");
var locals = magic() // TODO What should we do here?
alert(locals["myVariable"]); // returns myVariable
}
Just a note: JavaScript being evaluated comes from a trusted source.
eval()looks like? In other words, can you change that code at all in order to make this work, or are you stuck with code coming from another source, and you cannot change it?eval()does not create a new scope, and as far as I know there's no way to get a reference to your local "closure" scope in a way that'd let you treat it like an object.