【发布时间】:2018-02-07 08:55:56
【问题描述】:
在下面的代码中,在 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();
【问题讨论】:
-
this是 词法解析。 IE。它指的是定义obj1的环境的this的值。在那里,this不指代obj1。