js里的this指向_js instanceof

(2) 2024-08-23 19:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
js里的this指向_js instanceof,希望能够帮助你!!!。

this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定,this最终指向调用它的对象。

1.函数调用模式

当一个函数并非一个对象的属性时,那么它就是被当做函数来调用的。在此种模式下,this被绑定为全局对象,在浏览器环境下就是window对象

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张js里的this指向_js instanceof_https://bianchenghao6.com/blog__第2张

2.方法调用模式

当函数被保存为一个对象的属性时,它就可称为这个对象的方法。当一个方法被调用时,this被绑定到这个对象上。如果调用表达式包含一个提取属性的动作(. 或 []),那么它被称为方法调用

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张js里的this指向_js instanceof_https://bianchenghao6.com/blog__第4张

这里的this指向的对象是o,因为调用这个sayName()函数是通过o.sayName()执行的。

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张js里的this指向_js instanceof_https://bianchenghao6.com/blog__第6张

因为是o.b调用的这个函数,所以指向b这个对象

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张js里的this指向_js instanceof_https://bianchenghao6.com/blog__第8张

同理,因为是o.b调用的这个函数,所以指向b这个对象

 js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张js里的this指向_js instanceof_https://bianchenghao6.com/blog__第10张

t是全局变量,在全局环境下执行,this指向window

3.构造函数调用模式

如果在一个函数前面加上new关键字来调用,那么就会创建一个连接到该函数的prototype成员的新对象,同时,this会被绑定到这个新对象上。这种情况下,这个函数就可以成为此对象的构造函数。

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张js里的this指向_js instanceof_https://bianchenghao6.com/blog__第12张

在构造函数,new出一个对象时,this指向这个构造函数,new关键字会改变this的指向

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张js里的this指向_js instanceof_https://bianchenghao6.com/blog__第14张

当用new关键字,返回的是一个对象,this指向的就是那个返回的对象;

如果返回的不是对象,this还是指向函数的实例,虽然null属于对象,但是返回null依然指向函数实例

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第15张

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张js里的this指向_js instanceof_https://bianchenghao6.com/blog__第18张

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张js里的this指向_js instanceof_https://bianchenghao6.com/blog__第20张

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张js里的this指向_js instanceof_https://bianchenghao6.com/blog__第23张

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第24张

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第25张

4.apply和call调用模式

JS中,函数也是对象,所有函数对象都有两个方法:apply和call,这两个方法可以让我们构建一个参数数组传递给调用函数,也允许我们改变this的值

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张js里的this指向_js instanceof_https://bianchenghao6.com/blog__第27张

在全局范围内,this指向全局对象(浏览器下指window对象)

对象函数调用时,this指向当前对象

全局函数调用时,应该是指向调用全局函数的对象。

使用new关键字实例化对象时,this指向新创建的对象

当用apply和call上下文调用的时候指向传入的第一个参数

练习题

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第29张

在执行person1.sayName()时,时方法调用模式,this代表person1这个对象

在执行person2.sayName()时,时方法调用,但是sayName,并没有执行,而是将sayName()这个函数赋值给fun这个变量,fun是函数调用模式,this指向window,故输出全局的name

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第30张


js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张

执行console.log(b.n)时,b对象有自己的属性n值

执行console.log(c.n)时,c对象没有自己的属性n值,会向上查找,找的A对象中的属性n值

js里的this指向_js instanceof_https://bianchenghao6.com/blog__第1张js里的this指向_js instanceof_https://bianchenghao6.com/blog__第33张

vargetColor=test.getColor相当于把方法函数赋值给全局变量,

故getColor()中的this指向window

test.getColor()是方法调用

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复