目标
把this赋值给that,得出that依然可以等同于多个实例对象的结论
构造函数的this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
let that
function Star(uname){
that = this
// console.log(this)
this.uname = uname
}
const ldh = new Star('刘德华')
console.log(that === ldh) //true
const zxy = new Star('张学友')
console.log(that === zxy) //true
</script>
</body>
</html>
prototype的this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
let that
function Star(uname){
// that = this
this.uname = uname
}
// 原型对象里面的函数this指向的还是实例对象
Star.prototype.sing = function (){
console.log('唱歌')
that = this
// console.log(this) //当前实例对象
}
const ldh = new Star('刘德华')
ldh.sing()
console.log(that === ldh) //true
const zxy = new Star('张学友')
zxy.sing()
console.log(that === zxy) //true
</script>
</body>
</html>
结论
原型对象的this与构造函数的this指向同一个实例对象,可以说两个this完全相同