JavaScript多继承的实现
2018-07-20 来源:open-open
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);
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
下一篇:solr的查询语法
最新资讯
热门推荐