怎样通过混入(Mixin)实现多继承

js不提供现成的多重继承的方法, 但可以通过Object.assign()来手动实现: 

function Father1(name){
    this.name = name;
}

function Father2(age){
    this.age = age;
}

function Son(name, age){
    Father1.call(this,name);
    Father2.call(this,age);
}

Son.prototype = Object.create(Father1.prototype);
Object.assign(Son.prototype, Father2.prototype);

Son.prototype.constructor = Son;
var lilei = new Son("Lilei",23);
lilei.name; // "Lilei"
lilei.age; // 23

 

上一篇:Vue-cli 中安装并使用less


下一篇:如何在Vue 中管理 Mixins