Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138192

Specify scope for eval() in JavaScript?

$
0
0

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!


Viewing all articles
Browse latest Browse all 138192

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>