| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jope3014
8年前发布

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);