我正在学习关于这个标识符,我知道,当一个函数是不是在对象上调用那么这指的是在非严格模式的窗口对象。因此,我期望this.bar记录“无论如何”。为什么Chrome控制台中的结果与崇高不同?这JS
“无所谓”是输出,当我跑我在Chrome console.it代码,但输出是不确定的,当我使用节点构建系统崇高我的代码运行。
为什么是这样呢?我确定Chrome控制台的结果是正确的吗?还有什么时候可以遇到这样的问题?
这里是我的代码
function foo() {
// console.log(this)
console.log(this.bar);
}
var bar = "whatever";
// --------
foo(); // OUTPUT is "whatever" in the Chrome console and OUTPUT is undefined in Sublime's Node build system.
可能重复[“this”关键字是如何工作的?](http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – Claies