function inheritPrototype (subType, superType) {
subType.prototype = Object.create(superType.prototype)
subType.prototype.constractor = subType
}
function SuperType (name) {
this.name = name
}
SuperType.prototype.sayName = function () {
console.log(this.name);
}
function SubType () {
SuperType.call(this, "zhuyinghui")
this.age = 23
}
inheritPrototype(SubType, SuperType)
// SubType.prototype = new SuperType()
// 组合继承的缺点:SuperType会被调用两次
let subType = new SubType()
console.log(subType.name);
js引用类型继承最佳模式:寄生式组合继承
于 2021-05-26 00:03:20 首次发布