Kurt Z: Vererbungshierarchie ?!?

Beitrag lesen

Hi Struppi,

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

Naja ... wenn Crockford argumentiert sein Stil wäre lesbarer, dann hast du es ja gerade bewiesen. Deine Vorlage war IMHO schwer zu lesen, undokumentiert und zudem fehlerhaft.

Aber diese neue Vorlage ist schon deutlich weniger überladen:

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();
}

Was mir hier noch fehlt ist ein setter für die private Variabel color,

Na genau analog zum getter zu schreiben, die makeFunktion liefert dir den Scope  für die Closures die normalerweise von der constructorFunktion käme, um private Variablen und Funktionen zu realisieren. Eventuell noch zusätzlich ein

var color = c;

damit der setter die Variable findet. (theoretisch gehts aber ohne)

ausserdem wollte ich dem Basisobjekt (das bei dir a heißt) einen Parameter übergeben.

[code lang=javascript]function MyBaseObject(id) {
var obj = document.getElementById(id) || {style: {}};

this.obj = function() {return obj;}
this.setStyle = function(name, value) { obj.style[name] = value;};
}

  
ungetestet:  
  
~~~javascript
  
function makeBaseObject(id) {  
  var  that={};  
  var  node = document.getElementById(id) || {style: {}};  
  that.getNode = function() {return node;} ;  
  that.setStyle = function(name, value) { obj.style[name] = value;};  
  return that;  
}  

ich habe mal deine Namensgebung lesbarere gemacht, man sollte nicht alles obj nennen.

OK so?

gruß
 Kurt

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