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);
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/8781.html