留个名,看到再看。 Well, the Ecma spec considers a reference to eval to be a "direct eval call", but an expression that merely yields eval to be an indirect one -- and indirect eval calls are guaranteed to execute in global scope.
通过这个问题,我发现了一本新书《Effective JavaScript》,可惜在国外买中文版需要空运,那个大神能帮忙找找 pdf 的中文版额
《Effective JavaScript》中对eval有解释,在现代浏览器中间接调用eval会使用全局作为执行上下文,例如var myEval=eval;然后使用myEval,这样会失去eval的使用当前执行上下文的特性。 var $ = 1;
是说这种写法查找global作用域.但是用ie内核试了一下发现两个都是3. 估计不同内核的解释不一样, 所以...少用吧
(1, eval)('...')
(eval, eval)('...')
(1 ? eval : 0)('...')
(__ = eval)('...')
var e = eval; e('...')
(function(e) { e('...') })(eval)
(function(e) { return e })(eval)('...')
(function() { arguments[0]('...') })(eval)
this.eval('...')
this['eval']('...')
[eval][0]('...')
eval.call(this, '...')
eval('eval')('...')那么,根据ES5的标准,间接调用方式保证代码运行在全局作于域内,既然如此,自然输出1具体介绍,你可以查看 http://perfectionkills.com/global-eval-what-are-the-options/#evaling_in_global_scope
Well, the Ecma spec considers a reference to eval to be a "direct eval call", but an expression that merely yields eval to be an indirect one -- and indirect eval calls are guaranteed to execute in global scope.
var $ = 1;
(function() {
var $ = 2;
(function() {
var $ = 3;
(eval)('console.log($)'); //这里显示 3
(0, eval)('console.log($)'); //这里显示 1,(0,eval)这么写为什么会查找顶级作用域的变量? //因为这等价于:
var temp = (0,eval);
temp('console.log($)'); //temp使用了global作为执行上下文,而非当前的执行上下文
})();
})();
http://kuai.xunlei.com/d/hjQECcTLkL0iUwQA161版主、管理员一生平安!!!!谢谢!