Objects as Array Indices ?

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

    Objects as Array Indices ?

    Can someone explain why JavaScript arrays are not allowed to have objects as
    indices ?

    Would solve many a hashtable lookup problems....

    --
    Richard A. DeVenezia


  • Richard Cornford

    #2
    Re: Objects as Array Indices ?

    "Richard A. DeVenezia" <radevenz@ix.ne tcom.com> wrote in message
    news:bjmod7$kpm vs$1@ID-168040.news.uni-berlin.de...[color=blue]
    >Can someone explain why JavaScript arrays are not allowed to
    >have objects as indices ?
    >
    > Would solve many a hashtable lookup problems....[/color]

    Object property names are strings. If a reference to an object is used
    as - objectRef[anotherObjectRe f] - the anotherObjectRe f reference would
    be type converted to a string and the result would be used as the
    property name. That property name would be the implementation defined
    result of the Object.prototyp e.toString method (if not specifically
    overridden) and would often be a string along the lines of "[object]" or
    "Object [object]", so all object references would refer to the same
    property. Providing an object with its own toString method that returned
    a string unique to the object instance (eg "MyObject_inst1 22") might
    allow object references to be used to index JavaScript objects.

    Arrays may (normally would) have elements that may be referred to by
    integer index.

    Richard.


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Objects as Array Indices ?

      "Richard A. DeVenezia" <radevenz@ix.ne tcom.com> writes:
      [color=blue]
      > Can someone explain why JavaScript arrays are not allowed to have objects as
      > indices ?[/color]

      Because they can only have strings.

      Objects are mappings from property *names* to property values.
      I see no reason why it
      [color=blue]
      > Would solve many a hashtable lookup problems....[/color]

      If you want a hash table, you can probably make one using objects.
      But there is no reason to build generic hash table functionality
      into all objects.

      Putting as hash method on objects would be a simpler way to achieve
      the same effect.

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Objects as Array Indices ?

        "Richard Cornford" <richard@litote s.demon.co.uk> writes:
        [color=blue]
        > Arrays may (normally would) have elements that may be referred to by
        > integer index.[/color]

        They are still converted to strings before being used as indices.
        It's is just that some string indices, the ones that represent integers
        in normal form, also have an effect on the length property.

        Example:
        var x = [];
        x[2]=4;
        x["02"]=5;
        alert(x["2"]+x["02"]);
        This shows that "02" and "2" are different indices, but "2" and 2 are
        the same.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Richard Cornford

          #5
          Re: Objects as Array Indices ?

          "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
          news:smn5vv4s.f sf@hotpop.com.. .[color=blue]
          >snip>
          >They are still converted to strings before being used as indices.
          >It's is just that some string indices, the ones that represent
          >integers in normal form, also have an effect on the length
          >property.[/color]

          Have a go at confirming that specifically in IE. For example, try -
          for(var x in anArray) - and test typeof - x -.

          Richard.


          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Objects as Array Indices ?

            "Richard Cornford" <richard@litote s.demon.co.uk> writes:

            [Array indices are strings][color=blue]
            > Have a go at confirming that specifically in IE. For example, try -
            > for(var x in anArray) - and test typeof - x -.[/color]

            Testing the following code in IE6 gives four times a typeof of "string":
            ---
            var x=[1,2];
            x[2]=3;
            x["02"]=4
            for (var i in x) {
            alert (i+"("+(typeof i)+")="+x[i]);
            }
            ---
            So, I'll consider it confirmed for IE6 :)

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Douglas Crockford

              #7
              Re: Objects as Array Indices ?

              > Can someone explain why JavaScript arrays are not allowed to have objects as[color=blue]
              > indices ?[/color]

              Your question is a little sloppy. Object and arrays are closely related and
              distinct. Arrays should only be used when indices are integers. Objects should
              be used in all other cases.

              Member names in objects must be strings. Names are converted to strings before
              storage in the object. That's just how it works. This is a limitation for some
              applications. For example, it is difficult to write a serializer for cyclical
              structures.



              Comment

              • Richard Cornford

                #8
                Re: Objects as Array Indices ?

                "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
                news:oextvtqs.f sf@hotpop.com.. .[color=blue]
                >[Array indices are strings][color=green]
                >> Have a go at confirming that specifically in IE. For
                >>example, try - for(var x in anArray) - and test typeof - x -.[/color]
                >[/color]
                <snip>[color=blue]
                > So, I'll consider it confirmed for IE6 :)[/color]

                Yes, I got the wrong browser, it is Netscape 4 that does it wrong. It
                caused the problem in this thread:-

                <URL:
                http://www.google.com/groups?threadm...30fa79f%40news.
                demon.co.uk >

                - I knew IE was connected with it but it was in fact the browsers on
                which the (erroneous) code was failing because IE was returning strings.
                (That was back in April so my memory of it was fading.)

                Not that I am saying that they shouldn't be strings. I only mentioned
                Arrays using integer indexes to highlight that they may not be the
                appropriate object type on which to be storing properties by name (given
                a standard object as an alternative).

                Richard.


                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: Objects as Array Indices ?

                  "Richard Cornford" <richard@litote s.demon.co.uk> writes:
                  [color=blue]
                  > Yes, I got the wrong browser, it is Netscape 4 that does it wrong. It
                  > caused the problem in this thread:-
                  >
                  > <URL:
                  > http://www.google.com/groups?threadm...30fa79f%40news.
                  > demon.co.uk >[/color]

                  Indeed. And Netscape 4 does return other enumerable properties of an array,
                  and they are strings. It is only the properties with names that are integers
                  in normal form (no prefixed zeros) that are converted and retained as numbers.
                  [color=blue]
                  > - I knew IE was connected with it but it was in fact the browsers on
                  > which the (erroneous) code was failing because IE was returning strings.
                  > (That was back in April so my memory of it was fading.)[/color]

                  Can't say much for mine at all, and I did write something in the thread :)
                  [color=blue]
                  > Not that I am saying that they shouldn't be strings. I only mentioned
                  > Arrays using integer indexes to highlight that they may not be the
                  > appropriate object type on which to be storing properties by name (given
                  > a standard object as an alternative).[/color]

                  Not that it matters. Netscape 4 also uses numbers for integer indicies
                  of objects. Personally, I think the K.I.S.S. principle is reason enough
                  not to use arrays if you don't need them.

                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • Richard Cornford

                    #10
                    Re: Objects as Array Indices ?

                    "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
                    news:d6e8z446.f sf@hotpop.com.. .
                    <snip>[color=blue][color=green]
                    >>Arrays ... may not be the appropriate object type
                    >>on which to be storing properties by name (given
                    >>a standard object as an alternative).[/color]
                    >
                    > ... . Personally, I think the K.I.S.S. principle is reason
                    >enough not to use arrays if you don't need them.[/color]

                    I wouldn't argue with that.

                    Richard.


                    Comment

                    Working...