Tim Tepaße: Der Hash in Prototype

Beitrag lesen

Hallo Siechfred,

Es gibt bereits eine Klasse hash

Der Hash von Prototype tut auch nichts anderes als ein JS-Objekt als Hash zu nutzen, eben weil die interne JS-Implementierung (abseits des in der Aufruforder danach kommenden Prototype-Systems) mit Hashtabellen arbeitet. Prototype nutzt in seiner Implementierung nicht mal die Überprüfung von woher des jeweilige Objekt-Property kommt; aber damit muss man wohl in einem Framework leben, das das Originalsystem mit einer eigenen API so umbastelt, dass angenommen wird, das Originalsystem werde nicht mehr benutzt.

Ich würde es z.B. so oder so ähnlich basteln, würde ich auf der sicheren Seite sein wollen:

~~~javascript function Hash () {
      var store = {};

this.set = function (key, value) {
          store[key] = value;
      }

this.get = function (key) {
          if (store.hasOwnProperty(key)) {
              return store[key];
          } else {
              return undefined;
          }
      }

this.del = function (key) {
          delete store[key];
      }

this.each = function (func) {
          ...
      }
  }

  
(Man verliert natürlich das Zugreifen auf den durch Closure geschützten store bei zusätzlichen Prototype-Methoden, aber ich wollte ja nur das Grundprinzip demonstrieren.)  
  
Letztendlich haben beide Parteien hier im Thread recht. JS-Objekte sind im Sinne von „Watschelt wie eine Ente, quakt wie eine Ente“ Hashes; haben aber noch zusätzliche Semantik durch die prototypische Vererbung; sind praktisch eine Obermenge von Hashes. Wie man das nun genau interpretiert ist nun mal Interpretationssache und ist eine Frage des Standpunktes und der jeweiligen Bedürfnisse.  
  
  
Tim
0 139

was bewirkt {}

Ric
  • javascript
  1. 0
    Don P
  2. 0
    KurtZ
    1. 0
      Don P
      1. 0
        KurtZ
        1. 0
          Don P
          1. 0
            KurtZ
            1. 0
              Don P
              1. 0
                KurtZ
                1. 0
                  Cheatah
                  1. 0
                    KurtZ
                    1. 0
                      Don P
                      1. 0
                        KurtZ
                        1. 0
                          Siechfred
                    2. 0
                      Cheatah
        2. 0
          Don P
          1. 0
            KurtZ
            1. 0
              Don P
              1. 0
                KurtZ
    2. 0
      Cheatah
      1. 0
        KurtZ
        1. 0
          Siechfred
          1. 0
            KurtZ
            1. 0
              Cheatah
              1. 0
                KurtZ
                1. 1
                  Siechfred
                  1. 0
                    KurtZ
                    1. 0
                      Siechfred
                      1. 0
                        KurtZ
                        1. 0
                          Don P
                      2. 0
                        Siechfred
                      3. 2
                        Daniel Thoma
                        1. 0
                          KurtZ
                        2. 0
                          Siechfred
                          1. 0
                            Daniel Thoma
                            1. 0
                              Struppi
      2. 0

        "Herrje" Cheatah

        KurtZ
        1. 0
          Don P
          1. 0
            KurtZ
            1. 1
              Don P
              1. 0
                KurtZ
                1. 0
                  Don P
                  1. 0
                    KurtZ
        2. 0
          Siechfred
          1. 0
            KurtZ
            1. 0
              KurtZ
              1. 0
                Christoph
                1. 0
                  KurtZ
                  1. 0
                    Christoph
                    1. 0
                      LanX
                      1. 0
                        LanX
                        1. 0
                          Don P
                          1. 0
                            KurtZ
                            1. 0
                              Don P
                      2. 0
                        molily
                        1. 0
                          Christoph
                          1. 0
                            KurtZ
                            1. 0
                              Struppi
                            2. 0
                              Struppi
                            3. 0
                              Christoph
                              1. 0
                                Don P
                              2. 0
                                molily
                                1. 0
                                  molily
                              3. 0

                                "Herrje" Christoph

                                KurtZ
                                1. 0
                                  KurtZ
                                  1. 0
                                    Christoph
                                    1. 0
                                      KurtZ
                                      1. 0
                                        molily
                          2. 0
                            LanX²
                        2. 0
                          LanX²
                          1. 0
                            molily
                            1. 0
                              LanX²
                              1. 0
                                molily
                                1. 0
                                  KurtZ
                                  1. 0
                                    molily
                                    1. 0
                                      KurtZ
                              2. 0
                                KurtZ
                                1. 0
                                  LanX²
                    2. 0
                      molily
                      1. 0
                        LanX
                        1. 0
                          molily
                      2. 0
                        Christoph
            2. 0
              Siechfred
              1. 1

                Lieber SIechfred!

                KurtZ
                1. 0
                  Struppi
                  1. 0
                    Don P
                  2. 0
                    KurtZ
                    1. 0
                      Struppi
                      1. 0
                        KurtZ
                        1. 0
                          Struppi
                          1. 0

                            Lieber Struppi!

                            KurtZ
                          2. 1
                            Christoph
                            1. 0
                              Struppi
                            2. 0

                              Lieber Christof ;)

                              KurtZ
                              1. 0
                                KurtZ
                              2. 1
                                Christoph
                              3. 0

                                Lieber Christo__ph__ ;)

                                Christoph
                                1. 0
                                  KurtZ
                2. 0
                  Siechfred
                  1. 0

                    Lieber Siechfred!

                    KurtZ
                    1. 0
                      molily
                      1. 0
                        KurtZ
                3. 0
                  KurtZ
          2. 0

            Der Hash in Prototype

            Tim Tepaße
        3. 0
          Cheatah
          1. 0
            KurtZ
            1. 0
              Cheatah
      3. 0

        Übrigens, JS hat auch keine Arrays!

        KurtZ
        1. 0
          Don P
        2. 0
          Struppi
          1. 0
            KurtZ
            1. 0
              Struppi
              1. 0
                KurtZ
                1. 0
                  KurtZ
                2. 0
                  Struppi
                  1. 0
                    KurtZ
                    1. 0
                      Struppi
                      1. 0
                        KurtZ
                        1. 0
                          molily
                    2. 1
                      Don P
                      1. 0
                        KurtZ
                      2. 0
                        molily
                  2. 1
                    molily
        3. 2
          molily
        4. 0
          Harlequin
    3. 3
      KurtZ
    4. 0
      Struppi
      1. 0
        KurtZ
        1. 0
          molily
          1. 0
            KurtZ
            1. 0
              Christoph
              1. 0
                KurtZ
                1. 0
                  KurtZ
  3. 0

    Assoziative Arrays - es gibt sie doch!

    Christoph
    1. 0

      leicht modifiziert

      Christoph
      1. 0

        Iteratoren zur Realisierung von »reinen« Hashes nutzen

        Tim Tepaße
        1. 0
          Christoph
          1. 0
            Tim Tepaße
    2. 0
      Struppi