Struppi: Vererbungshierarchie ?!?

Beitrag lesen

Ich würds vielleicht so machen:

function object (o){
  function F(){}
  F.prototype=o;
  return new F();
};

function maker (o,color){
  var child=object(o);
  child.Color=function (){return color};
  return child;
};

x={a:1};
y=maker(x,"blau");
z=maker(x,"grün");
alert(y.Color()+z.Color()+y.color); //blaugrünundefined

  
Was mir hier noch fehlt ist ein setter für die private Variabel color, ausserdem wollte ich dem Basisobjekt (das bei dir a heißt) einen Parameter übergeben.  
  
~~~javascript
function MyBaseObject(id) {  
 var obj = document.getElementById(id) || {style: {}};  
  
 this.obj = function() {return obj;}  
 this.setStyle = function(name, value) { obj.style[name] = value;};  
}  
  
function MyChildObject(id, c) {  
 function F() {  
  var color = c;  
  this.getColor = function() { return color;}  
  this.setColor = function(c) { color = c; this.setStyle('color', c); return color;}  
 }  
 F.prototype = new MyBaseObject(id);  
 return new F();  
}  
  
var o1 = new MyChildObject('test_1', 'red');  
var o2 = new MyChildObject('test_2', 'green');  
o1.setColor('blue');  
alert(o1.getColor() );  

Die Frage von mir war ja, wie kann ein komplexeres Objekt (mein Beispiel ist noch relativ einfach), mit dieser Funktion erzeugen.

Struppi.

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