首先我想這一點 -爲什麼「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 ) –