function Person(obj) {
this.name = obj.name;
this.age = obj.age;
}
Person.prototype.log = function (value) {
console.log(`${this.name} is ${this.age} is ${value}`)
}
var p1 = new Person({
name: 'aaa',
age: 25
})
p1.log('hello') // aaa is 25 is hello
function P2(obj) {
Person.call(this, obj)
}
P2.prototype = Object.create(Person.prototype) // !!!!
P2.prototype.constructor = Person // !!!!
var p2 = new P2({
name: 'sss',
age: 27
})
p2.log('world') // sss is 27 is world
手写javascript系列之寄生式的组合继承
于 2022-03-11 20:43:30 首次发布