首先我想这一点 -为什么“this”在胖箭头函数定义中未定义?
const profile = {
name: 'Alex',
getName: function(){
return this.name;
}
};
工作正常。现在我用胖箭头尝试同样的事情。在这种情况下,这个“未来”是未定义的。
const profile = {
name: 'Alex',
getName:() => {
return this.name;
}
};
这给了我一个错误
TypeError: Cannot read property 'name' of undefined
我的教训是,胖箭头语法是更好的方式处理隐含的“本”。请解释为什么会发生这种情况。
是的,这个问题不回答这个问题:http://stackoverflow.com/questions/31095710/methods-in-es6-objects-using-arrow-functions –
HTTPS://developer.mozilla。 org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions – Dellirium
也是[函数与函数声明/表达式的重复:它们是否等同/可交换?](http://stackoverflow.com/q/34361379/218196 ) –