Objects as Array Indices ?

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

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

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

Richard Cornford

Richard A. DeVenezia said:
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.
 
L

Lasse Reichstein Nielsen

Richard A. DeVenezia said:
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
 
L

Lasse Reichstein Nielsen

Richard Cornford said:
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
 
R

Richard Cornford

Lasse Reichstein Nielsen said:
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.
 
L

Lasse Reichstein Nielsen

[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);
}
 
D

Douglas Crockford

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
 
R

Richard Cornford

Lasse Reichstein Nielsen said:
[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 -.
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.
 
L

Lasse Reichstein Nielsen

Richard Cornford said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top