箭头函数中的“this”与对象字面量中的非箭头函数

Pet*_*ner 8 javascript ecmascript-6

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

Dan*_*sky 5

根据定义,箭头函数的行为与传统的不同。使用() => {}语法定义的函数从外部作用域继承上下文。