【发布时间】:2016-11-30 01:31:06
【问题描述】:
首先我尝试了这个 -
const profile = {
name: 'Alex',
getName: function(){
return this.name;
}
};
效果很好。现在我用胖箭头尝试了同样的事情。在那种情况下,“this”是未定义的。
const profile = {
name: 'Alex',
getName: () => {
return this.name;
}
};
这给了我一个错误
TypeError: 无法读取未定义的属性“名称”
我学到的是,粗箭头语法可以更好地处理隐含的“this”。请解释为什么会这样。
【问题讨论】:
-
是的,这个问题确实回答了它:stackoverflow.com/questions/31095710/…