封裝/繼承/多態是面向對象編程的三個特征, js中實現構造函數的繼承需要分兩步實現:
1. 在子類構造函數中調用父類的構造函數;
2. 讓子類的原型對象"復制"父類的原型對象;
下面是一個具體的例子:
function Father(name){ this.name = name; } function Son(name, age){ Father.call(this, name); this.age = age; } Son.prototype = Object.create(Father.prototype); Son.prototype.constructor = Son; var lilei = new Son("Lilei", 23); lilei.name; // "Lilei" lilei.age; // 23 lilei.constructor === Son; // true