SOURCE

function Animal(name) {
    this.name = name
    this.colors = ['black', 'white']
}
Animal.prototype.getName = function() {
    return this.name
}
function Dog(name, age) {
    Animal.call(this, name)
    this.age = age
}
//Dog.prototype =  new Animal()
//Dog.prototype.constructor = Dog

function F(){}
F.prototype = Animal.prototype
let f =  new F()
f.constructor = Dog
Dog.prototype = f

function object(o) {
    function F() {}
    F.prototype = o
    return new F()
}
function inheritPrototype(child, parent) {
    let prototype = object(parent.prototype)
    prototype.constructor = child
    child.prototype = prototype
}
inheritPrototype(Dog, Animal)

// Dog.prototype =  Object.create(Animal.prototype)
// Dog.prototype.constructor = Dog

 

let dog1 = new Dog('奶昔', 2)
dog1.colors.push('brown')
let dog2 = new Dog('哈赤', 1)
console.log(dog2) 
// { name: "哈赤", colors: ["black", "white"], age: 1 }

/**
 * 组合继承结合了原型链和盗用构造函数,
 * 将两者的优点集中了起来。
 * 基本的思路是使用原型链继承原型上的属性和方法,
 * 而通过盗用构造函数继承实例属性。
 * 这样既可以把方法定义在原型上以实现重用,又可以让每个实例都有自己的属性。
 */

class Animal {
    constructor(name) {
        this.name = name
    } 
    getName() {
        return this.name
    }
}
class Dog extends Animal {
    constructor(name, age) {
        super(name)
        this.age = age
    }
}

console 命令行工具 X clear

                    
>
console