Hi
wenn du NEW in JS anwendest, ist einer der Schritte den prototype des Constructors im "__proto__" Attribut der Instance zu verlinken:
INSTANCE= new CONSTRUCTOR();
; => INSTANCE["__proto__"]=CONSTRUCTOR.prototype;
Vererbung funtioniert dann durch ein schrittweises durchsuchen der
"Prototype-Chain" in den "__proto__" attributen.
also in Pseudocode linear programmiert:
function getAttr(Attr,ThisObj) {
// ? local (this) vorhanden ???
Obj=ThisObj;
if (Obj.Attr)
{return Obj.Attr};
// ? im Prototype des Elter CONSTRUCTOR vorhanden ???
Obj=ThisObj["__proto__"];
if (Obj.Attr)
{return Obj.Attr};
// ? im Prototype des Großelter vorhanden ??
Obj=ThisObj["__proto__"]["__proto__"];
// identisch mit CONSTRUCTOR.prototype["__proto__"]
if (Obj.Attr)
{return Obj.Attr};
// ... usw bis es kein Eltern"__Proto__" mehr gibt
}
man hangelt sich also durch die prototype Attribute der "Konstruktoren" die die üblichen "Klassendefinition" ersetzen, ähnlich ISA in Perl.
deswegen schreibt man auch zwecks Vererbung von SUPER zu SUB
SUB.prototype=new SUPER;
womit implizit
SUB.prototype.__proto__=SUPER.prototype
ACHTUNG: SUB.__proto__ wird nicht geändert, es ist sein Prototype Unterobjekt welches Instance des vererbenden SUPER-Konstruktors wird.
OK, soweit der Mechanismus nun zu deinem Casus cnacktus:
class1.prototype = new class0();
instance.prototype = new class1();
=>
class1.prototype.__proto__=class0.prototype;
instance.prototype.__proto__=class1.prototype;
funktioniert!
instance.prototype = new class1();
=> instance.prototype.__proto__=class1.prototype;
class1.prototype = new class0();
class1.prototype.__proto__=class0.prototype;
MOMENT hier hast du class1.prototype neu von class0 instanziert und mit einem neuen Objekt überschrieben! instance.prototype.__proto__ verweist aber noch auf das alte Objekt.
Ich bin zugegeben selbst verwirrt ... leider weiß ich auch gerade nicht wie man in JS eine explizite Objekt_ID eines Objektes abfragt um das Verhalten zu überprüfen. ... habe jetzt auch keine anderen Browser da.
Bye
LanX