Objects as Array Indices ?

Discussion in 'Javascript' started by Richard A. DeVenezia, Sep 10, 2003.

  1. 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 A. DeVenezia, Sep 10, 2003
    #1
    1. Advertising

  2. "Richard A. DeVenezia" <> wrote in message
    news:bjmod7$kpmvs$-berlin.de...
    >Can someone explain why JavaScript arrays are not allowed to
    >have objects as indices ?
    >
    > Would solve many a hashtable lookup problems....


    Object property names are strings. If a reference to an object is used
    as - objectRef[anotherObjectRef] - the anotherObjectRef 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.prototype.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_inst122") 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.
     
    Richard Cornford, Sep 10, 2003
    #2
    1. Advertising

  3. "Richard A. DeVenezia" <> writes:

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


    Because they can only have strings.

    Objects are mappings from property *names* to property values.
    I see no reason why it

    > Would solve many a hashtable lookup problems....


    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 -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Sep 10, 2003
    #3
  4. "Richard Cornford" <> writes:

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


    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 -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Sep 10, 2003
    #4
  5. "Lasse Reichstein Nielsen" <> wrote in message
    news:...
    >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.


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

    Richard.
     
    Richard Cornford, Sep 10, 2003
    #5
  6. "Richard Cornford" <> writes:

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


    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);
    }
    ---
    So, I'll consider it confirmed for IE6 :)

    /L
    --
    Lasse Reichstein Nielsen -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Sep 10, 2003
    #6
  7. > Can someone explain why JavaScript arrays are not allowed to have objects as
    > indices ?


    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.

    http://www.crockford.com/javascript/survey.html
     
    Douglas Crockford, Sep 10, 2003
    #7
  8. "Lasse Reichstein Nielsen" <> wrote in message
    news:eek:...
    >[Array indices are strings]
    >> Have a go at confirming that specifically in IE. For
    >>example, try - for(var x in anArray) - and test typeof - x -.

    >

    <snip>
    > So, I'll consider it confirmed for IE6 :)


    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=b8blun$hfl$1$830fa79f@news.
    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.
     
    Richard Cornford, Sep 10, 2003
    #8
  9. "Richard Cornford" <> writes:

    > 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=b8blun$hfl$1$830fa79f@news.
    > demon.co.uk >


    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.

    > - 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.)


    Can't say much for mine at all, and I did write something in the thread :)

    > 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).


    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 -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Sep 10, 2003
    #9
  10. "Lasse Reichstein Nielsen" <> wrote in message
    news:...
    <snip>
    >>Arrays ... may not be the appropriate object type
    >>on which to be storing properties by name (given
    >>a standard object as an alternative).

    >
    > ... . Personally, I think the K.I.S.S. principle is reason
    >enough not to use arrays if you don't need them.


    I wouldn't argue with that.

    Richard.
     
    Richard Cornford, Sep 10, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin
    Replies:
    1
    Views:
    483
    Gunnar Hjalmarsson
    Jun 3, 2004
  2. b83503104
    Replies:
    3
    Views:
    4,022
    Al Bowers
    May 21, 2004
  3. KLaus Blank
    Replies:
    1
    Views:
    457
  4. Christof Warlich
    Replies:
    4
    Views:
    320
    Christof Warlich
    Aug 9, 2008
  5. Jason S

    objects with string indices

    Jason S, Nov 16, 2006, in forum: Javascript
    Replies:
    7
    Views:
    119
    Richard Cornford
    Nov 21, 2006
Loading...

Share This Page