【发布时间】:2017-10-17 13:47:04
【问题描述】:
如果在当前范围内没有找到变量,JS引擎会寻找父分数,直到它到达全局范围。
现在,如果我在下面尝试,在浏览器中
console.log(someRandomVariable);
// This throws ReferenceError as someRandomVariable is Not found on Global/Window scope.
但是当我在窗口对象上显式调用它时,它显示为未定义。
console.log(window.someRandomVariable)
根据我的理解,前者也在搜索窗口对象,因为它不在任何函数范围内。
为什么它有两种不同的行为?
【问题讨论】: