TS-9 面向对象-原型
// new的时候 js 会自动帮你把 this.__proto__ = Person.prototype
function Person(name,age){
this.name = name
this.age = age
}
Person.prototype = {
constructor: Person,
sayHi(target){
console.log(`你好${target.name},我是${this.name}`)
}
}
const p1 = new Person('frank')
const p2 = new Person('ricky')
p1.sayHi(p2)
![](https://img.haomeiwen.com/i17903226/d375dc4d06280be1.png)
90ed730907c7f84dfbeefe80d50e6ec.png
- 这里的
prototype
是为了给 const obj = new f1()
生成的对象用的,即obj.__proto__ === f1.prototype
8a0321b94fd4954b814f79db69774ea.png
本文标题:TS-9 面向对象-原型
本文链接:https://www.haomeiwen.com/subject/gbzmkdtx.html
网友评论