其他分享
首页 > 其他分享> > 怎样通过混入(Mixin)实现多继承

怎样通过混入(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

 

标签:lilei,混入,name,继承,age,Father2,Son,Mixin,prototype
来源: https://www.cnblogs.com/aisowe/p/11672364.html