JavaScript多繼承的實現
function BaseClass1(){
this.age=10;
this.sex='male';
}
BaseClass1.prototype.run = function(){
console.log("run");
}
function BaseClass2(){
this.prop1='prop1';
this.prop2='prop2';
}
BaseClass2.prototype.walk = function(){
console.log("walk");
}
function ChildClass(country,hobby){
this.country=country;
this.hobby=hobby;
//實例屬性繼承
BaseClass1.call(this);
BaseClass2.call(this);
}
//原型鏈繼承
for(var prop in BaseClass1.prototype){
ChildClass.prototype[prop] = BaseClass1.prototype[prop];
}
for(var prop in BaseClass2.prototype){
ChildClass.prototype[prop] = BaseClass2.prototype[prop];
}
// ChildClass.prototype.constructor=ChildClass;
var childInstance = new ChildClass('China','ball');
childInstance.run();
childInstance.walk();
console.log(childInstance.prop1);
console.log(childInstance.age);</pre>
本文由用戶 jope3014 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!