有没有什么办法可以在特定范围内执行eval()(但不是全局)?在JavaScript中指定eval()的范围?
例如,下面的代码无法正常工作(一个未定义的第二条语句),因为它们是在不同的范围:
eval(var a = 1);
eval(alert(a));
如果可能的话,我想即时创建一个范围。例如(语法肯定是不对的,但只是为了说明的想法)
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
}
如何实现这样的事情你知道吗?谢谢!
如果有人仍然感兴趣,我只是在这里发布一个答案http://stackoverflow.com/a/43306962/1758245 – 2017-04-09 13:06:11