在下面的代码中,在 obj1 的对象字面量中,我假设两个函数中的“this”都将引用 obj1,但在粗箭头函数中,它不是。有人可以解释为什么吗?我会假设这些函数要么是等效的,要么在胖箭头函数中,'this' 将在词法上定义为 obj1。
var obj1 = {
name : 'name1',
standardFunction : function() {
console.log(this.name); // Refers to obj1
},
fatArrowFunction : () => { // Refers to the global object
console.log(this.name);
}
}
obj1.standardFunction();
obj1.fatArrowFunction();
Run Code Online (Sandbox Code Playgroud)