var a,b,c;
(function(){
eval('var b = 2');
(1, eval)('var c = 3');
(new Function('var a = 4'))();
document.write('
a: ' + a);
document.write('
b: ' + b);
document.write('
c: ' + c);
})()
document.write('
a: ' + a);
document.write('
b: ' + b);
document.write('
c: ' + c);
执行此段代码后,得到如下结果
a: undefined
b: 2
c: 3
a: undefined
b: undefined
c: 3
http://codepen.io/quietcoder/...
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
eval能访问上下文,new Function只能构建自己的一个私有作用域。
更新,注释一下