“this”关键字确实用lamda引用当前对象

时间:2017-08-16 05:59:43

标签: javascript lambda

考虑以下代码:

var obj = {
   method : function(){
      console.log( this ); // This prints the **obj** correctly
   }
};

与Lambda相同的代码:

var obj = {
   method : () => {
        console.log( this ); // This prints **Window** object
    };
};

为什么输出不同?

1 个答案:

答案 0 :(得分:0)

ES6箭头函数语法使用“词法范围”来确定“this”的值应该是什么。词汇范围是一种奇特的方式,它说它使用来自周围代码的“this”...包含相关代码的代码,因此它是

  

窗口

这里。

相关问题