// An highlighted block
/*
构造函数相关的this指向
1:构造函数体内的this指向
=> 因为和new关联字连用,this 指向当前实例
2:构造函数原型上的方法里面的 this 指向
=> 因为方法是依靠实例对象在调用
=> this 指向当前实例
*/
function Person(){
this.name='无名'
console.log('构造函数体内:' ,this)
}
Person.prototype.fn=function(){
console.log('我是 Person.prototype 上的 fn 方法')
console.log('Person.prorotype.fn:', this )
}
// 创建实例
let p1=new Person()
// 调用方法
// fn 是 Person.prorotype 上的方法
// 当时调用是依靠当前实例在调用
// 标准的对象调用方式
// p1.fn() this 点 前面是谁就是水
p1.fn() // this=> 当前实例
面向对象08 构造函数相关this指向
最新推荐文章于 2023-03-18 20:24:24 发布