js基础复习6-原型对象中的this
发布日期:2021-05-07 01:06:32 浏览次数:26 分类:原创文章

本文共 555 字,大约阅读时间需要 1 分钟。

<!DOCTYPE html><html><head>	<title></title></head><body>	<script type="text/javascript">		function Rabbit (name, color) {   			this.name = name			this.color = color		}		var that		Rabbit.prototype.eat = function () {   			that = this			console.log('i eat carrot😋')		}		var r1 = new Rabbit('Amanda', 'white')		r1.eat()		console.log(that)		console.log(that === r1)	</script></body></html>

1.在构造函数中,里面的this指向的是对象实例, 比如Rabbit构造函数中,this指向的就是创建实例对象r1时候的r1

2. 在Rabbit.prototype.eat这个对象原型的方法里面,this指向的是实例对象r1,因为r1可以调用eat()方法, 注意!这里this的指向是在函数被调用时候确定的

我们运行代码,看看结果:
在这里插入图片描述

上一篇:js基础复习7-原型对象用途-利用原型对象扩展内置对象方法
下一篇:【游记】准信心赛

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年04月04日 18时28分35秒