1
我有下面的代碼,其中有兩個函數a,c和c正在從a繼承。在調用派生類函數時出現javascript util.inherits問題
function a(){
console.log("constructor-a");
}
a.prototype.fun1 = function(){
console.log("a-fun1");
}
a.prototype.fun2 = function(){
console.log("a-fun2");
}
function c(){
c.super_.call(this)
console.log("constructor-c");
}
c.prototype.fun5 = function(){
console.log("c-fun1");
}
c.prototype.fun6 = function(){
console.log("c-fun2");
}
util.inherits(c,a);
var X = new c();
console.log("X instanceof a", X instanceof a);
console.log("X instanceof a", X instanceof c);
由於C是從繼承,既C的功能和一個應執行的從C的對象,但是,在這種情況下,輸出如下:
X.fun1() --> a-fun1
X.fun2() --> a-fun2
X.fun5() --> TypeError: X.fun5 is not a function
X.fun6() --> TypeError: X.fun6 is not a function
'util.inherits'需要在分配原型之前調用。 – Bergi