猿问

在回调函数中如果使用箭头函数怎么获取相应的this?

举个例子,用jQuery

$('#test').on('click', ()=>{

    console.log(this) // window

});

那我要怎么获取外部和内部的this?除了将箭头函数换成原来的function就没有其他方法了吗?



PIPIONE
浏览 703回答 1
1回答

慕妹3146593

es6中说到,箭头函数中没有自己的this:this指向的固定化,并不是因为箭头函数内部有绑定this的机制,实际原因是箭头函数根本没有自己的this官方也有说明:Arrow functions capture the this value of the enclosing context所有箭头函数通过call或者apply调用时传this也是没有效果的
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答