this在ES5的function函數中可以指代一個obj

const obj2 = {
username: May,
hello: function(){
console.log(this)
return hello + this.username
}
}
console.log(obj2.hello());

this在箭頭函數中refer to Windows

const obj2 = {
username: May,
hello: ()=>{
console.log(this)
return hello + this.username
}
}
console.log(obj2.hello());

針對this,用箭頭函數和普通函數是有區別的哦, 記住啦

推薦閱讀:

相关文章