1 //组合继承是Javascript最常用的继承模式 2 function SuperType(name) { 3 this.name = name; 4 this.colors = ["red", "blue", "green"]; 5 } 6 7 SuperType.prototype.sayName = function() { 8 console.log(this.name); 9 }; 10 11 function SubType(name, age) { 12 //继承属性 13 SuperType.call(this, name); 14 this.age = age; 15 } 16 17 //继承方法 18 SubType.prototype = new SuperType(); 19 20 SubType.prototype.sayAge = function() { 21 console.log(this.age); 22 }; 23 24 var instance1 = new SubType("Nicholas", 29); 25 instance1.colors.push("black"); 26 console.log(instance1.colors); 27 instance1.sayName(); 28 instance1.sayAge(); 29 30 var instance2 = new SubType("Greg", 27); 31 console.log(instance2.colors); 32 instance2.sayName(); 33 instance2.sayAge();