function Parent (name) {
this.name = name;
this.say = () => {
console.log('我的名字是' + this.name);
};
}
Parent.prototype.play = () => {
console.log('打篮球');
};
function Children (name) {
Parent.call(this);
this.name = name;
}
Children.prototype = Object.create(Parent.prototype);
Children.prototype.constructor = Children;
let child = new Children('张三');
child.say();
child.play();
console.log(child.name);
寄生组合继承的优点:
- 只调用一次父类构造函数
- Child可以向Parent传参
- 父类方法可以复用
- 父类的引用属性不会被共享 上述关于寄生组合继承的实现是自己学习的记录,更详细的实现方法大家可以看一下JS继承 原型链继承、构造函数继承、组合继承、原型继承、寄生式继承、寄生组合继承 - 掘金 (juejin.cn)