【发布时间】:2017-04-19 08:48:10
【问题描述】:
let objNewWay = {
width:400,
height:300,
area: () => this.width*this.height
};
console.log(objNewWay.area()); // NaN
let objOldWay = {
width:400,
height:300,
area: function() {
return this.width*this.height;
}
};
console.log(objOldWay.area()); // 120000
我不明白为什么 Javascript 对象中的箭头函数似乎不起作用。如果你看上面的代码,第一个 console.log 打印 NaN,第二个打印出预期的数字。
【问题讨论】:
-
它叫Arrow functions不是lamda
-
来自 MDN:两个因素影响了箭头函数的引入:更短的函数和
this的非绑定。 b> -
谢谢。我已将问题从 lambda 更改为数组函数。
-
arrow 函数,而不是 array 函数。
标签: javascript ecmascript-6 arrow-functions