首先我试过这个 -
const profile = {
name: 'Alex',
getName: function(){
return this.name;
}
};
哪个工作正常。现在我用胖箭头尝试了同样的事情。在这种情况下,“this”将是未定义的。
const profile = {
name: 'Alex',
getName: () => {
return this.name;
}
};
这给了我一个错误
类型错误:无法读取未定义的属性“名称”
我学到的是,粗箭头语法可以更好地处理隐式“this”。请解释为什么会发生这种情况。