Ich habe den folgenden Code, wo gibt es zwei Funktionen a, c und c erbt von a.Javascript util.herherits Problem beim Aufruf abgeleiteter Klassenfunktionen
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);
Da C wird aus einem vererben, die Funktionen von sowohl C als auch a sollte von einem Objekt von C, jedoch in diesem Fall ausführbar sind die Ausgänge wie folgt:
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" muss vor Zuweisungen an den Prototyp aufgerufen werden. – Bergi