• 怎样通过混入(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
  • 相关阅读:
    【leetcode】图像渲染
    【leetcode】不邻接植花
    052-75
    052-74
    052-73
    052-71
    052-70
    052-69
    052-67
    052-66
  • 原文地址:https://www.cnblogs.com/aisowe/p/11672364.html
Copyright © 2020-2023  润新知