var x = 'outer';
(function() {
var x = 'inner';
eval('console.log("direct call: " + x)');
(1,eval)('console.log("indirect call: " + x)');
})();
输出
direct call: inner
indirect call: outer
原因:
- (1,eval)是个表达式,;类似1&&eval,属于间接引用了eval;
- eval 只在被直接调用时,this指向才是当前作用域,否则则是全局作用域;
stackoverflow: https://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript
参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/eval
网友评论