Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Objects as Array Indices ?

Reply
Thread Tools

Objects as Array Indices ?

 
 
Richard A. DeVenezia
Guest
Posts: n/a
 
      09-10-2003
Can someone explain why JavaScript arrays are not allowed to have objects as
indices ?

Would solve many a hashtable lookup problems....

--
Richard A. DeVenezia


 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      09-10-2003
"Richard A. DeVenezia" <(E-Mail Removed)> wrote in message
news:bjmod7$kpmvs$(E-Mail Removed)-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.


 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-10-2003
"Richard A. DeVenezia" <(E-Mail Removed)> 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-10-2003
"Richard Cornford" <(E-Mail Removed)> 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 - (E-Mail Removed)
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      09-10-2003
"Lasse Reichstein Nielsen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.


 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-10-2003
"Richard Cornford" <(E-Mail Removed)> 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[i]);
}
---
So, I'll consider it confirmed for IE6

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Douglas Crockford
Guest
Posts: n/a
 
      09-10-2003
> 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

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      09-10-2003
"Lasse Reichstein Nielsen" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
>[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...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.


 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-10-2003
"Richard Cornford" <(E-Mail Removed)> 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...30fa79f%40news.
> 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 - (E-Mail Removed)
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      09-10-2003
"Lasse Reichstein Nielsen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
<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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to instantiate array of objects knowing its indices at compiletime? Christof Warlich C++ 4 08-09-2008 09:22 PM
Using an array value as indices for an array KLaus Blank VHDL 1 07-07-2008 07:38 PM
objects with string indices Jason S Javascript 7 11-21-2006 05:02 PM
using a string as array indices Justin Perl 1 06-02-2004 11:38 PM
how to efficiently do sorting and get array of indices? b83503104 C Programming 3 05-21-2004 10:44 AM



Advertisments