先分享一篇this指向的文章,我觉得写的不错
一、普通函数的this指向一共分三种:(this始终指向调用它的对象)
1、指向window
在JavaScript 函数中,函数的拥有者默认绑定 this。
因此,全局作用域或者普通函数自执行中this指向全局对象window
(JavaScript 严格模式不允许默认绑定。因此,在严格模式下,this 是未定义的(undefined)。)

2、对象方法调用时,this指向调用的对象
在对象方法中,this 指的是此方法的“拥有者”。

在上述例子中,this 指的是 obj对象。obj对象是 fn方法的拥有者。
3、事件函数内部的this,指向事件源

this的打印结果为:

4.改变this指向
利用call(), bind(), apply()。如:

二、箭头函数的this指向
1.箭头函数的this永远指向其上下文的 this,任何方法都改变不了其指向,如call(), bind(), apply()
(找作用域)
网友评论