LanX²: __proto__ chain

Beitrag lesen

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

0 67

Vererbungshierarchie ?!?

Kurt Z
  • javascript
  1. 0
    Struppi
    1. 0
      molily
    2. 0
      Don P
      1. 0
        Kurt Z
        1. 0
          Struppi
          1. 0
            Don P
            1. 0
              Struppi
              1. 0
                Don P
                1. 0
                  Struppi
                  1. 0
                    Don P
                    1. 0
                      Struppi
                      1. 0
                        Don P
                        1. 0
                          Struppi
                          1. 0
                            Don P
                      2. 0
                        Kurt Z
                        1. 0
                          Struppi
                          1. 0
                            Kurt Z
                            1. 0
                              Struppi
                              1. 0
                                Kurt Z
                                1. 0
                                  Struppi
                                  1. 0
                                    Kurt Z
                                    1. 0
                                      LanX²
                                    2. 0
                                      Struppi
                                      1. 0
                                        Kurt Z
                                        1. 0
                                          Kurt Z
                                        2. 0
                                          Struppi
                                          1. 0
                                            Kurt Z
                              2. 0
                                Don P
              2. 0
                LanX²
                1. 0

                  Function.prototype.begetObject

                  LanX²
                  1. 0
                    Don P
                    1. 0

                      Knobelaufgabe

                      LanX²
                      1. 0
                        Don P
                    2. 0
                      molily
                      1. 0
                        Kurt Z
                        1. 0

                          Please beget it!

                          LanX²
                          1. 0
                            Kurt Z
                          2. 0
                            Don P
                            1. 0
                              LanX²
                              1. 0
                                Don P
                              2. 0

                                Iterator

                                KurtZ
                                1. 0
                                  LanX
                      2. 0
                        Don P
        2. 0

          new Object(x) statt object(x)

          Kurt Z
          1. 0
            Murphy
            1. 0
              Kurt Z
              1. 0
                Don P
              2. 0
                Murphy
                1. 0
                  Kurt Z
                2. 0
                  molily
  2. 0
    LanX²
  3. 0
    molily
  4. 3

    __proto__ chain

    LanX²
    1. 0
      LanX²
      1. 0
        Kurt Z
    2. 1
      molily
  5. 0

    JS-Version?

    Kurt Z
    1. 0
      Kurt Z
      1. 0
        molily
        1. 0
          molily
    2. 0
      molily
      1. 0
        Kurt Z
        1. 0
          molily
          1. 0
            Kurt Z
  6. 0

    VIELEN DANK!!!

    Kurt Z
    1. 0
      Struppi