// 组合继承
function Parent(value) {
this.val = value;
}
Parent.prototype.getValue = function () {
console.log(this.val)
}
function Child(value) {
Parent.call(this, value);
}
Child.prototype = new Parent();
const child = new Child(1)
child.getValue()
console.log(child instanceof Parent)
// 寄生组合继承
function Animal(name) {
this.name = name;
}
Animal.prototype.callName = function () {
console.log(this.name)
}
function Cat(name) {
Animal.call(this, name)
}
Cat.prototype = Object.create(Animal.prototype, {
constructor: {
value: Cat,
enumerable: false,
writable: true,
configurable: true
}
})
const cat = new Cat('大橘')
cat.callName()
console.log(cat instanceof Animal)
console