<分区>
在下面的代码中,在 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();