var Hybi = function(request, url, options) { Base.apply(this, arguments); }; util.inherits(Hybi, Base);
如题,直接inherits,为何还要调用父类的构造方法
4 回复
> console.log(util.inherits.toString())
function (ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
}
util.inherits将prototype加上了。这样才可以用父类的方法。
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里面注释的一行打开过后,这里就不报错了。