Array as hash tables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Cornford

    #16
    Re: Array as hash tables

    rh wrote:[color=blue]
    > "Richard Cornford" wrote:[/color]
    <snip>[color=blue][color=green]
    >> If you want to store key/value pairs without any restrictions to the
    >> names perhaps you need to implement an object for that storage.[/color]
    >
    > That's unfortunate. Truthfully, this is a name-scope conflict with
    > genesis imbedded as a deficiency of the language (a statement made
    > notwithstanding the fact that JavaScript remains, by far, my favourite
    > progamming language). It should be possible to obtain a basic object
    > that is entirely clear of such name conflicts.
    >
    > Given that, and ever seemingly the contrarian, I don't believe that
    > Alexis is that far off track in attempting to remedy the deficiency
    > through removal of pre-definded values in the object. ...[/color]

    We have seen that Mozilla is admitting to adding some non-standard
    properties to its JS objects. I am fairly sure that I have also seen
    some similar non-standard properties on Konqueror objects, as apparently
    exposed implementation details (I don't have time to re-check right
    now). That makes relying on blanking a list of expected properties
    unsatisfactory, as it would be difficult to guarantee that the list was
    comprehensive across JS implementations .

    Adding a space to the front of the property names is not guaranteed to
    be absolutely safe either, but the chances of coincidence are lower and
    programmers are unlikely to make JS implementations that used space
    prefixed property names (not least because most other languages would
    not allow it).

    Richard.


    Comment

    • John G Harris

      #17
      Re: Array as hash tables

      In article <d3526$4113e8c6 $43656322$30595 @msgid.meganews servers.com>,
      Douglas Crockford <nospam@covad.n et> writes[color=blue][color=green][color=darkred]
      >>>I finally came to use the following as a naked object, to use as a
      >>>hashtable[/color][/color]
      >[color=green]
      >> Why do you call it a hashtable? It could just as easily be a balanced
      >> tree or even a simple list. You wouldn't be able to tell the difference.
      >> If you want to be posh, and accurate too, call it an associative
      >>array.
      >> If you want to be less posh you could call it a map.[/color]
      >
      >And if you want to be understood in a JavaScript forum, you would call
      >it an object.[/color]

      But I thought the whole point of this thread was that a javascript
      object isn't good enough to be an any name in, correct value out kind of
      thing, or map as we might call it.

      Also I'm surprised no-one has suggested prefixing each name with
      something unlikely, such as "TQ7_".

      John
      --
      John Harris

      Comment

      • rh

        #18
        Re: Array as hash tables

        "Richard Cornford" wrote:[color=blue]
        > rh wrote:[color=green]
        > > "Richard Cornford" wrote:[/color][/color]
        <..>[color=blue]
        > We have seen that Mozilla is admitting to adding some non-standard
        > properties to its JS objects. I am fairly sure that I have also seen
        > some similar non-standard properties on Konqueror objects, as apparently
        > exposed implementation details (I don't have time to re-check right
        > now).[/color]

        I wasn't aware of that, but it does go right to the heart of the
        design problem that exists. Under the current model of objects having
        a single namespace, it isn't possible to extend native properties of
        the JS object without potentially breaking existing programs.

        Of course, given prototype inheritance, the single namespace is far
        more a blessing than a curse.
        [color=blue]
        > That makes relying on blanking a list of expected properties
        > unsatisfactory, as it would be difficult to guarantee that the list was
        > comprehensive across JS implementations .
        >[/color]

        Yes, so now I want to be provided with a native method that will
        return a list of property names that manifest through the prototype
        chain :).

        Or perhaps, to obtain a completely unencumbered object, I just want to
        be able to turn off the prototype chain processing?

        ../rh

        Comment

        • Lasse Reichstein Nielsen

          #19
          Re: Array as hash tables

          John G Harris <john@nospam.de mon.co.uk> writes:
          [color=blue]
          > But I thought the whole point of this thread was that a javascript
          > object isn't good enough to be an any name in, correct value out kind of
          > thing, or map as we might call it.[/color]

          It works fine for that. The problem is that it doesn't start out empty,
          but wnything you put in, you get out again fine.
          [color=blue]
          > Also I'm surprised no-one has suggested prefixing each name with
          > something unlikely, such as "TQ7_".[/color]

          Richard did. He suggested a single space IIRC.


          Ok, an associative array implementation that isn't fooled by standard
          or non-standard values:
          ---
          function Map() {
          this.map = {};
          }
          Map.Value = function Value(v) {
          this.value = v;
          }
          Map.prototype.c ontainsKey = function containsKey(k) {
          return (this.map[k]) instanceof Map.Value;
          }
          Map.prototype.p ut = function put(k,v) {
          this.map[k] = new Map.Value(v);
          };
          Map.prototype.g et = function get(k) {
          var v = this.map[k];
          if (v instanceof Map.Value) {
          return v.value;
          }
          };
          Map.prototype.r emove = function remove(k) {
          delete this.map[k]
          };
          ---

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Richard Cornford

            #20
            Re: Array as hash tables

            rh wrote:[color=blue]
            > Richard Cornford wrote:[/color]
            <snip>[color=blue]
            > Or perhaps, to obtain a completely unencumbered object, I just
            > want to be able to turn off the prototype chain processing?[/color]

            The implication in the ECMA specified [[construct]] method is that
            either an object must be assigned to the internal [[prototype]] property
            of a newly created object or the original Object.prototyp e will be used
            (not even the current Object.prototyp e). I don't see any way of
            acquiring a reference to an object with less than one object on its
            prototype chain.

            Richard.


            Comment

            • Lasse Reichstein Nielsen

              #21
              Re: Array as hash tables

              "Richard Cornford" <Richard@litote s.demon.co.uk> writes:
              [color=blue]
              > I don't see any way of acquiring a reference to an object with less
              > than one object on its prototype chain.[/color]

              Object.prototyp e :)
              But apart from that, no.
              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • Richard Cornford

                #22
                Re: Array as hash tables

                Lasse Reichstein Nielsen wrote:[color=blue]
                > Richard Cornford writes:
                >[color=green]
                >> I don't see any way of acquiring a reference to an
                >> object with less than one object on its prototype chain.[/color]
                >
                > Object.prototyp e :)
                > But apart from that, no.[/color]

                Good point. That is one object with a null [[prototype]] property.
                Unfortunately it will also feature the list of specified properties and
                any implementation dependent properties from the outset so it doesn't
                help in this context.

                Richard.


                Comment

                • Thomas 'PointedEars' Lahn

                  #23
                  Re: Array as hash tables

                  Lasse Reichstein Nielsen wrote:
                  [color=blue]
                  > John G Harris <john@nospam.de mon.co.uk> writes:[color=green]
                  >> But I thought the whole point of this thread was that a javascript
                  >> object isn't good enough to be an any name in, correct value out kind of
                  >> thing, or map as we might call it.[/color]
                  >
                  > It works fine for that. The problem is that it doesn't start out empty,
                  > but wnything you put in, you get out again fine.[/color]

                  With the exception of built-in properties, see below.
                  [color=blue][color=green]
                  >> Also I'm surprised no-one has suggested prefixing each name with
                  >> something unlikely, such as "TQ7_".[/color]
                  >
                  > Richard did. He suggested a single space IIRC.
                  >
                  >
                  > Ok, an associative array implementation that isn't fooled by standard[/color]
                  ^^^^^^^^^^^^^^^ ^^^^^^^^^[color=blue]
                  > or non-standard values:[/color]
                  ^^^^^^^^^^^^^^^ ^^^^^^^
                  What is this supposed to mean?
                  [color=blue]
                  > ---
                  > function Map() {
                  > this.map = {};
                  > }
                  > Map.Value = function Value(v) {
                  > this.value = v;
                  > }
                  > Map.prototype.c ontainsKey = function containsKey(k) {
                  > return (this.map[k]) instanceof Map.Value;[/color]

                  It does not make sense to put the operand of `instanceof' in parantheses.
                  It would make more sense to put the entire return value in parantheses
                  although this would not be required as `instanceof' has precedence over
                  `return'.
                  [color=blue]
                  > }
                  > Map.prototype.p ut = function put(k,v) {
                  > this.map[k] = new Map.Value(v);
                  > };
                  > Map.prototype.g et = function get(k) {
                  > var v = this.map[k];
                  > if (v instanceof Map.Value) {[/color]
                  ^^^^^^^^^^^^^^^ ^^^^^^^
                  Would it not be wise to use this.contains(v ) here?
                  [color=blue]
                  > return v.value;
                  > }
                  > };
                  > Map.prototype.r emove = function remove(k) {
                  > delete this.map[k]
                  > };[/color]

                  Your implementation is still missing the point: Objects have built-in
                  properties that are often not enumerable but could still be overwritten
                  (including methods!) and thus assignment to objectReference[k] (here:
                  this.map[k]) must not be allowed for those values of k. Encapsulating
                  the value in another (prototyped) object (twice) does not really help
                  here, and if the used object contains _only_ a value property this
                  approach is merely a waste of memory. Only a suitable transparent
                  modification of the passed property name (suffix, prefix, infix; while
                  one the former to should be used) will solve that problem. A single
                  space indeed seems to be viable as internal prefix, yet the prototype
                  could be implemented more flexible in the case the space is not enough:

                  function Map() {}

                  Map.prototype.p refix = " ";
                  Map.prototype.s uffix = "";

                  Map.prototype.c ontainsKey = function map_containsKey (k)
                  {
                  return !!(this[this.prefix + k + this.suffix]);
                  }

                  Map.prototype.p ut = function map_put(k, v)
                  {
                  this[this.prefix + k + this.suffix] = v;
                  };

                  Map.prototype.g et = function map_get(k)
                  {
                  return this[this.prefix + k + this.suffix];
                  };

                  Map.prototype.r emove = function map_remove(k)
                  {
                  delete this[this.prefix + k + this.suffix];
                  };


                  PointedEars

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #24
                    Re: Array as hash tables

                    Thomas 'PointedEars' Lahn wrote:
                    [color=blue]
                    > Lasse Reichstein Nielsen wrote:[color=green]
                    >> Map.prototype.c ontainsKey = function containsKey(k) {
                    >> return (this.map[k]) instanceof Map.Value;[/color]
                    > [...][color=green]
                    >> }
                    >> Map.prototype.p ut = function put(k,v) {
                    >> this.map[k] = new Map.Value(v);
                    >> };
                    >> Map.prototype.g et = function get(k) {
                    >> var v = this.map[k];
                    >> if (v instanceof Map.Value) {[/color]
                    > ^^^^^^^^^^^^^^^ ^^^^^^^
                    > Would it not be wise to use this.contains(v ) here?[/color]

                    No, it would not, and this.containsKe y(k) would result in another
                    unnessary lookup which is not reasonable. Forget about this.


                    PointedEars

                    Comment

                    • Lasse Reichstein Nielsen

                      #25
                      Re: Array as hash tables

                      Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                      [color=blue]
                      > Lasse Reichstein Nielsen wrote:
                      >[color=green]
                      >> Ok, an associative array implementation that isn't fooled by standard[/color]
                      > ^^^^^^^^^^^^^^^ ^^^^^^^^^[color=green]
                      >> or non-standard values:[/color]
                      > ^^^^^^^^^^^^^^^ ^^^^^^^
                      > What is this supposed to mean?[/color]

                      Hmm ... should be: fooled by existing standard or non-standard
                      properties.
                      [color=blue][color=green]
                      >> Map.prototype.r emove = function remove(k) {
                      >> delete this.map[k]
                      >> };[/color]
                      >
                      > Your implementation is still missing the point: Objects have built-in
                      > properties that are often not enumerable but could still be overwritten
                      > (including methods!)[/color]

                      Absolutely. I'm depending on them being overwritable.
                      [color=blue]
                      > and thus assignment to objectReference[k] (here: this.map[k]) must
                      > not be allowed for those values of k.[/color]

                      Yes it must. The object used as a map here (this.map) is *only* used
                      for this purpose. Any property that it has inherited should be ignore,
                      and can safely be overwritten. That is, *if* they can be overwritten.

                      If the properties are "magic", like, e.g., innerHTML on DOM nodes, or
                      "length" on Arrays, then even overwriting is a problem.

                      However, that would be a violation of ECMA 262, since an object is
                      created using "new Constructor", where "Constructo r" is a user defined
                      function, can not be a host object.

                      What you might be thinking of, is that deleting existing properties
                      should not be allowed, which is true (no "delete Object.prototyp e.toString").
                      So, "delete" should be:

                      Map.prototype.r emove = function remove(k) {
                      if (this.containsK ey(k)) {
                      delete this.map[k]
                      }
                      };
                      [color=blue]
                      > Encapsulating the value in another (prototyped) object (twice) does
                      > not really help here, and if the used object contains _only_ a value
                      > property this approach is merely a waste of memory.[/color]

                      No, the point of wrapping in an instance of Map.Value is that no
                      preexisting property is an instance of Map.Value. That allows us to
                      distinguish properties that we put in the map from those already
                      there, no matter what type they might be (including objects or
                      arrays).

                      The "containsKe y" and "get" methods (and the correct "delete") test
                      that the property value is an instance of Map.Value, i.e., that it's
                      something put there using the "put" method.

                      (I'm not considering someone deliberatly messing with the "this.map"
                      property, only the problem of avoiding mistakin existing properties
                      for elements of the map)
                      [color=blue]
                      > Only a suitable transparent modification of the passed property name
                      > (suffix, prefix, infix; while one the former to should be used) will
                      > solve that problem.[/color]

                      This solves it for correct ECMA262 implementations .

                      Modifying the property name blindly will not be absolutely safe, since
                      you might hit another preexisting property name with the modified name
                      as well (who knows, maybe some day, an ECMAScript impelentation will
                      have a hidden method called " hiddenMethod " on Object prototype).

                      /L
                      --
                      Lasse Reichstein Nielsen - lrn@hotpop.com
                      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                      'Faith without judgement merely degrades the spirit divine.'

                      Comment

                      • Grant Wagner

                        #26
                        Re: Array as hash tables

                        Lasse Reichstein Nielsen wrote:
                        [color=blue]
                        > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                        >[color=green]
                        > > Only a suitable transparent modification of the passed property name
                        > > (suffix, prefix, infix; while one the former to should be used) will
                        > > solve that problem.[/color]
                        >
                        > This solves it for correct ECMA262 implementations .
                        >
                        > Modifying the property name blindly will not be absolutely safe, since
                        > you might hit another preexisting property name with the modified name
                        > as well (who knows, maybe some day, an ECMAScript impelentation will
                        > have a hidden method called " hiddenMethod " on Object prototype).[/color]

                        How about defining the prefix/suffix combination by some millisecond time stamp /
                        psuedo-random value at run time. This would reduce the chances of a conflict to
                        zero[1]:

                        <script type="text/javascript">
                        function Map() {}
                        Map.prototype.p refix = (new Date()).getTime ();
                        Map.prototype.s uffix = Math.floor(Map. prototype.prefi x * Math.random()) + "_";
                        Map.prototype.p refix = "_" + prefix;
                        Map.prototype.u niqueId = prefix + suffix;
                        // or one of the endless variations on this concept
                        // ... etc ...
                        </script>

                        Doing it this way would also guarantee that you could uniquely[2] identify each
                        Map(), although I'm not sure there's any value in that.

                        [1] - almost zero. It's certainly possible, however unlikely, that a user agent
                        or future ECMAScript implementation might have an Object property called
                        "_1092061591623 toString1026459 394798_" or
                        "_1092061887384 constructor5278 75481827_".
                        [2] - almost unique. Due to system clock synchronizing software, it would be
                        possible, however unlikely, for two Map()s to have the same prefix and randomly
                        generate the same suffix.

                        --
                        Grant Wagner <gwagner@agrico reunited.com>
                        comp.lang.javas cript FAQ - http://jibbering.com/faq

                        Comment

                        • Thomas 'PointedEars' Lahn

                          #27
                          Re: Array as hash tables

                          Lasse Reichstein Nielsen wrote:
                          [color=blue]
                          > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=green]
                          >> Lasse Reichstein Nielsen wrote:[color=darkred]
                          >>> Ok, an associative array implementation that isn't fooled by standard[/color]
                          >> ^^^^^^^^^^^^^^^ ^^^^^^^^^[color=darkred]
                          >>> or non-standard values:[/color]
                          >> ^^^^^^^^^^^^^^^ ^^^^^^^
                          >> What is this supposed to mean?[/color]
                          >
                          > Hmm ... should be: fooled by existing standard or non-standard
                          > properties.[/color]

                          Well, it can still be fooled.
                          [color=blue][color=green][color=darkred]
                          >>> Map.prototype.r emove = function remove(k) {
                          >>> delete this.map[k]
                          >>> };[/color]
                          >>
                          >> Your implementation is still missing the point: Objects have built-in
                          >> properties that are often not enumerable but could still be overwritten
                          >> (including methods!)[/color]
                          >
                          > Absolutely. I'm depending on them being overwritable.[/color]

                          But some properties are read-only.
                          [color=blue][color=green]
                          >> and thus assignment to objectReference[k] (here: this.map[k]) must
                          >> not be allowed for those values of k.[/color]
                          >
                          > Yes it must. The object used as a map here (this.map) is *only* used
                          > for this purpose. Any property that it has inherited should be ignore,
                          > and can safely be overwritten. That is, *if* they can be overwritten.[/color]

                          If so, you would overlay core properties leading to unexpected behavior
                          because the respective prototype property will not be accessed anymore.

                          And if not, you would have saved nothing in your "hash table". That
                          does not sound like a (reasonable/viable) solution.
                          [color=blue]
                          > If the properties are "magic", like, e.g., innerHTML on DOM nodes, or
                          > "length" on Arrays, then even overwriting is a problem.
                          >
                          > However, that would be a violation of ECMA 262, since an object is
                          > created using "new Constructor", where "Constructo r" is a user defined
                          > function, can not be a host object.[/color]

                          This is not about host objects, it is about core properties *every*
                          object has.
                          [color=blue]
                          > What you might be thinking of, is that deleting existing properties
                          > should not be allowed,[/color]

                          No, although that would be useful.
                          [color=blue]
                          > which is true (no "delete Object.prototyp e.toString").[/color]

                          You would effectively overlay Object.prototyp e.toString if you would try
                          to add a "toString" hash table entry and cause to property cease to work
                          as supposed. Or consider a "valueOf" hash table entry. Or "constructo r".
                          Or ...
                          [color=blue][color=green]
                          >> Encapsulating the value in another (prototyped) object (twice) does
                          >> not really help here, and if the used object contains _only_ a value
                          >> property this approach is merely a waste of memory.[/color]
                          >
                          > No, the point of wrapping in an instance of Map.Value is that no
                          > preexisting property is an instance of Map.Value.[/color]

                          Even your Map and Map.Value instances have preexisting properties
                          inherited from Object. They may not be enumerable but they still
                          exist. There is no, I repeat, *no* object that has no "preexistin g
                          properties".
                          [color=blue]
                          > That allows us to distinguish properties that we put in the map from those
                          > already there, no matter what type they might be (including objects or
                          > arrays).[/color]

                          I won't debate that you can distinguish between added and built-in
                          properties and I did not. The point is that your implementation does
                          not prevent overlaying or overwriting built-in properties. You control
                          the read, but not the write process (yet).
                          [color=blue][color=green]
                          >> Only a suitable transparent modification of the passed property name
                          >> (suffix, prefix, infix; while one the former to should be used) will
                          >> solve that problem.[/color]
                          >
                          > This solves it for correct ECMA262 implementations .[/color]

                          No, it does not, because of

                          ,-[ECMA 262 Edition 3, 2000-03-24]
                          |
                          | 2 Conformance
                          |
                          | [...]
                          | A conforming implementation of ECMAScript is permitted to provide
                          | additional types, values, objects, properties, and functions beyond
                          | those described in this specification. In particular, a conforming
                          | implementation of ECMAScript is permitted to provide properties not
                          | described in this specification, and values for those properties,
                          | for objects that are described in this specification.

                          And unfortunately(? ), we are not living in a perfect standardized world.
                          Implementations make use of the opportunity described above, there is
                          escape(), unescape(), __proto__ and *much* more out there.

                          Exactly ECMA-262 and the prototype chain presents the reason why your
                          hash table implementation "as is" is flawed.


                          PointedEars

                          Comment

                          • Lasse Reichstein Nielsen

                            #28
                            Re: Array as hash tables

                            Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                            [color=blue][color=green]
                            >> Absolutely. I'm depending on them being overwritable.[/color]
                            >
                            > But some properties are read-only.[/color]

                            But they exist on the prototype, not on the object itself. They can be
                            overwritten on the inheriting object.

                            Assume o is an object and o.ro is a read-only, don't delete,
                            etc. property. Then
                            ---
                            function C(){};
                            C.prototype = o;
                            var c = new C();
                            c.ro = 42;
                            ---
                            will work and set "c.ro" to 42. It isn't affected by the read-only
                            property of the prototype. The prototype's property is just what you
                            read if you haven't created that property on the object itself.
                            [color=blue][color=green]
                            >> Yes it must. The object used as a map here (this.map) is *only* used
                            >> for this purpose. Any property that it has inherited should be ignore,
                            >> and can safely be overwritten. That is, *if* they can be overwritten.[/color]
                            >
                            > If so, you would overlay core properties leading to unexpected behavior
                            > because the respective prototype property will not be accessed anymore.[/color]

                            No unexpected behavior will happen, as long as the object is only used
                            to look up properties in. No "core property" affects how [[Get]] and
                            [[Put]] works.

                            Unexpected behavior might happen if I overwrite the "toString"
                            property *and* tries to convert the object to a string, but I never do
                            that.
                            [color=blue]
                            > And if not, you would have saved nothing in your "hash table". That
                            > does not sound like a (reasonable/viable) solution.[/color]

                            Not understood.
                            [color=blue]
                            > This is not about host objects, it is about core properties *every*
                            > object has.[/color]

                            That every object's *prototype* has. The object itself has no
                            properties.

                            Doing
                            new Object()
                            creates a new native ECMAScript object. It has no visible properties
                            itself, only the ones inherited from its [[Prototype]], Object.prototyp e.
                            [color=blue]
                            > You would effectively overlay Object.prototyp e.toString if you would try
                            > to add a "toString" hash table entry and cause to property cease to work
                            > as supposed.[/color]

                            Deliberatly. That's why I don't use the map for anything except property
                            lookup.
                            [color=blue]
                            > Or consider a "valueOf" hash table entry. Or "constructo r".
                            > Or ...[/color]

                            It is not a problem.
                            [color=blue][color=green]
                            >> No, the point of wrapping in an instance of Map.Value is that no
                            >> preexisting property is an instance of Map.Value.[/color]
                            >
                            > Even your Map and Map.Value instances have preexisting properties
                            > inherited from Object. They may not be enumerable but they still
                            > exist. There is no, I repeat, *no* object that has no "preexistin g
                            > properties".[/color]

                            Absolutely. What I was saying is that no preexisting property of the
                            object used as a map would be an instance of "Map.Value" . That means
                            that any property of the map object can be tested for being an
                            instance of "Map.Value" , and if it is, it's something I put there,
                            otherwise it should be ignored.
                            [color=blue]
                            > I won't debate that you can distinguish between added and built-in
                            > properties and I did not. The point is that your implementation does
                            > not prevent overlaying or overwriting built-in properties.[/color]

                            Agree completely. It depends on doing just that.
                            [color=blue]
                            > You control the read, but not the write process (yet).[/color]

                            Sure I do, unless the implementation is pathologically flawed.
                            [color=blue]
                            > No, it does not, because of
                            >
                            > ,-[ECMA 262 Edition 3, 2000-03-24]
                            > |
                            > | 2 Conformance[/color]
                            [color=blue]
                            > And unfortunately(? ), we are not living in a perfect standardized world.
                            > Implementations make use of the opportunity described above, there is
                            > escape(), unescape(), __proto__ and *much* more out there.[/color]

                            As long as a *new* object has no properties itself (not counting the ones
                            inherited from its prototype) that are read-only or don't-delete, then
                            it will work.

                            An implementation that makes a new object have a read-only or
                            don't-delete property that cannot be overwritten is asking for
                            trouble. Imagine

                            var x = new Object();
                            x.xyzzy = 42; // bang! "xyzzy" is read-only. Not a good idea!
                            [color=blue]
                            > Exactly ECMA-262 and the prototype chain presents the reason why your
                            > hash table implementation "as is" is flawed.[/color]

                            No, the prototype chain guarantees that the read-only or don't-delete
                            properties of the prototype can be overwritten in the inheriting
                            object.

                            /L
                            --
                            Lasse Reichstein Nielsen - lrn@hotpop.com
                            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                            'Faith without judgement merely degrades the spirit divine.'

                            Comment

                            • Thomas 'PointedEars' Lahn

                              #29
                              Re: Array as hash tables

                              Lasse Reichstein Nielsen wrote:
                              [color=blue]
                              > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=green][color=darkred]
                              >>> Yes it must. The object used as a map here (this.map) is *only* used
                              >>> for this purpose. Any property that it has inherited should be ignore,
                              >>> and can safely be overwritten. That is, *if* they can be overwritten.[/color]
                              >>
                              >> If so, you would overlay core properties leading to unexpected behavior
                              >> because the respective prototype property will not be accessed anymore.[/color]
                              >
                              > No unexpected behavior will happen, as long as the object is only used
                              > to look up properties in. No "core property" affects how [[Get]] and
                              > [[Put]] works.
                              >
                              > Unexpected behavior might happen if I overwrite the "toString"
                              > property *and* tries to convert the object to a string, but I never do
                              > that.[/color]

                              A toString() method, e.g., is really a nice-to-have for such hash tables
                              or collections, so I will try not to allow overlaying prototype properties
                              in my implementation (Collection in collection.js, available soon.)
                              [color=blue][color=green]
                              >> And if not, you would have saved nothing in your "hash table". That
                              >> does not sound like a (reasonable/viable) solution.[/color]
                              >
                              > Not understood.[/color]

                              Does not matter anymore.
                              [color=blue][color=green]
                              >> This is not about host objects, it is about core properties *every*
                              >> object has.[/color]
                              >
                              > That every object's *prototype* has. The object itself has no
                              > properties.
                              >
                              > Doing
                              > new Object()
                              > creates a new native ECMAScript object. It has no visible properties
                              > itself, only the ones inherited from its [[Prototype]], Object.prototyp e.[/color]

                              ACK
                              [color=blue][color=green]
                              >> You control the read, but not the write process (yet).[/color][/color]
                              ^^^^^[color=blue]
                              > Sure I do, unless the implementation is pathologically flawed.[/color]

                              I meant that you do not check if there is a prototype
                              property (yet) before over*writing*/overlaying it.
                              [color=blue]
                              > As long as a *new* object has no properties itself (not counting the ones
                              > inherited from its prototype) that are read-only or don't-delete, then
                              > it will work.[/color]

                              Hmmm. If you overlay `prototype', you would destroy
                              (the possibility of) inheritance, would you not?
                              [color=blue]
                              > An implementation that makes a new object have a read-only or
                              > don't-delete property that cannot be overwritten is asking for
                              > trouble. Imagine
                              >
                              > var x = new Object();
                              > x.xyzzy = 42; // bang! "xyzzy" is read-only. Not a good idea![/color]

                              Not a good idea for Object objects. Perfectly reasonable for others.
                              [color=blue][color=green]
                              >> Exactly ECMA-262 and the prototype chain presents the reason why your
                              >> hash table implementation "as is" is flawed.[/color]
                              >
                              > No, the prototype chain guarantees that the read-only or don't-delete
                              > properties of the prototype can be overwritten in the inheriting
                              > object.[/color]

                              ACK, but I would refrain from overlaying prototype properties anyway here.


                              PointedEars

                              Comment

                              • Lasse Reichstein Nielsen

                                #30
                                Re: Array as hash tables

                                Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                                [color=blue]
                                > I meant that you do not check if there is a prototype
                                > property (yet) before over*writing*/overlaying it.[/color]

                                Correct. It shouldn't matter, though.
                                [color=blue]
                                > Hmmm. If you overlay `prototype', you would destroy
                                > (the possibility of) inheritance, would you not?[/color]

                                No. The "prototype" property is only relevant on function objects, and
                                the map object ("this.map") is just a normal object. It probably
                                doesn't have a property called "prototype" , and if it does, it doesn't
                                do anything (not to be confuzed with the internal "[[Prototype]]"
                                property).
                                [color=blue][color=green]
                                >> An implementation that makes a new object have a read-only or
                                >> don't-delete property that cannot be overwritten is asking for
                                >> trouble.[/color][/color]
                                ....[color=blue]
                                > Not a good idea for Object objects. Perfectly reasonable for others.[/color]

                                Absolutely. Other types of Object, like those created by Array, will
                                have properties inherent to the specific object (e.g. "length"). Those
                                will be on the object itself, not its prototype, and can easily be
                                on-overwritable.
                                [color=blue]
                                > ACK, but I would refrain from overlaying prototype properties anyway here.[/color]

                                Out of curiosity: Why?

                                /L
                                --
                                Lasse Reichstein Nielsen - lrn@hotpop.com
                                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                                'Faith without judgement merely degrades the spirit divine.'

                                Comment

                                Working...