轉自 http://www.cnblogs.com/yangjinjin/archive/2013/02/01/2889563.htmlhtml
這一次要講 組合、原型式、寄生式、寄生組合式繼承方式。函數
1. 組合繼承:又叫僞經典繼承,是指將原型鏈和借用構造函數技術組合在一塊的一種繼承方式。this
下面來看一個例子:spa
function SuperType(name) { this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function() { alert(this.name); } function SubType(name, age) { SuperType.call(this, name); this.age = age; } //繼承方法 SubType.prototype = new SuperType(); SubType.prototype.sayAge = function() { alert(this.age); } var instance1 = new SubType("Nicholas", 29); instance1.colors.push("black"); alert(instance1.colors); //red,blue,green,black instance1.sayName(); //Nicholas instance1.sayAge(); //29 var instance2 = new SubType("Greg", 27); alert(instance2.colors); //red,blue,green instance2.sayName(); //Greg instance2.sayAge(); //27
組合繼承避免了原型鏈和借用構造函數的缺陷,融合它們的優勢。prototype
未完待續……code