有没有办法在一个特定的作用域(而不是全局的)上执行eval
例如,以下代码无法工作(第二条语句中未定义a),因为它们在不同的作用域中:
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
}
有没有关于如何实现这样的事情的想法?谢谢!
https://stackoverflow.com/questions/9781285
复制相似问题