問題描述:封裝了axios方法,getAxios()是成功時的callback為什么會出現下面代碼中的結果我的想法:1.()=>{} 等價于 function(){}.bind(this)2.在嚴格模式下,沒有直接調用者的函數中的this是 undefined自己隱約知道原因,但又不確定,希望能得到明朗的答案。 created () { this.getBannerList()
},
methods: {
getBannerList () {
getAxios('/bannerApi', (res) => { console.log(this) //結果:VueComponent對象
})
getAxios('/bannerApi', function (res) { console.log(this) //結果: undefined
})
}
}
2 回答

繁花如伊
TA貢獻2012條經驗 獲得超12個贊
問題描述:
封裝了axios方法,getAxios()是成功時的callback
為什么會出現下面代碼中的結果
我的想法:
1.()=>{} 等價于 function(){}.bind(this)
2.在嚴格模式下,沒有直接調用者的函數中的this是 undefined
自己隱約知道原因,但又不確定,希望能得到明朗的答案。
created () { this.getBannerList() }, methods: { getBannerList () { getAxios('/bannerApi', (res) => { console.log(this) //結果:VueComponent對象 }) getAxios('/bannerApi', function (res) { console.log(this) //結果: undefined }) } }
添加回答
舉報
0/150
提交
取消