請教下關於node繼承的問題,爲什麼使用了inherits還要在給構造方法裏面使用父類的構造方法

function ReadStream() {
stream.Readable.call(this);
}
util.inherits(ReadStream,stream.Readable);

------------------------------
var util = require('util'); var ConstructorA = function () { this.func1 = function () { console.log('im func1!!'); } }; ConstructorA.prototype.func2 = function () { console.log('im func2!!'); }; var ConstructorB = function () { //ConstructorA.call(this); }; util.inherits(ConstructorB, ConstructorA); var conb = new ConstructorB(); conb.func2();//正常。若是util.inherits(ConstructorB, ConstructorA);被註釋了,這句將報錯 conb.func1();//報錯,上面ConstructorB裏面註釋的一行打開事後,這裏就不報錯了。
不掉用 父類的構造方法 只能繼承原型鏈上的屬性。無法仿問 函數內定義的內部屬性。
相關文章
相關標籤/搜索