组合寄生

https://www.cnblogs.com/PrajnaParamita/p/5773783.html

 

更清楚理解组合寄生继承

function inheritPrototype(subType, superType){
    var protoType = Object.create(superType.prototype);    //创建对象
    protoType.constructor = subType;                    //因为object.create创建的只有一个__proto__属性
    subType.prototype = protoType;                        //指定对象  
}
function SuperType(name){
    this.name = name;
    this.colors = ["red", "blue", "green"];
}
SuperType.prototype.sayName = function(){
    alert(this.name);
}

function SubType(name, age){
    SuperType.call(this, name);  
    
    this.age = age;
}
inheritPrototype(SubType, SuperType)
SubType.prototype.sayAge = function(){
    alert(this.age);
}

var instance = new SubType("Bob", 18);
instance.sayName();
instance.sayAge();

 

posted @ 2020-06-09 20:14  cnchengv  阅读(9)  评论(0编辑  收藏  举报