473,765 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 20 '05 #1
9 2129
"Richard A. DeVenezia" <ra******@ix.ne tcom.com> wrote in message
news:bj******** ****@ID-168040.news.uni-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[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.
Jul 20 '05 #2
"Richard A. DeVenezia" <ra******@ix.ne tcom.com> 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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
"Richard Cornford" <ri*****@litote s.demon.co.uk> 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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:sm******** **@hotpop.com.. .
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.
Jul 20 '05 #5
"Richard Cornford" <ri*****@litote s.demon.co.uk> 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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
> 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

Jul 20 '05 #7
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:oe******** **@hotpop.com.. .
[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.
Jul 20 '05 #8
"Richard Cornford" <ri*****@litote s.demon.co.uk> 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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #9
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:d6******** **@hotpop.com.. .
<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.
Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1714
by: Nik Coughin | last post by:
I am having a problem with checkboxes... I have a number of blocks like this on my form: <input type="text" name="name" value="a name"> <input type="text" name="ddi" value="1234"> <input type="checkbox" name="delete"> If I then submit the form, the array $delete is the only one which isn't indexed by its order on the form.
26
1854
by: Sterten | last post by:
when I define int R; and then later access it with x=R;C=7; .... but x happens to be <0 or >99 , then the program's behavious becomes unpredictable. Is there a way to prevent this ? Is there a program or debugger or compiler which
6
1689
by: Sean Hamilton | last post by:
Hello, Suppose I have a pointer to some structures. What would be the correct type to store the number of entries in that array, as well as for indexing that array? size_t seems like an obvious choice, but off_t also jumps to mind. Thanks, --
3
2154
by: David Mathog | last post by:
This one is driving me slightly batty. The code in question is buried deep in somebody else's massive package but it boils down to this, two pointers are declared, the first is: char **resname which is part of the "atoms" structure that is passed into the function from the outside. I have not yet found where it is allocated but I'm reasonably sure from other chunks of this code that it was by:
6
1542
by: Scott Townsend | last post by:
So here is a noob question.. I'm looking to have Int16 SQLCmd Byte Array that is a Total of 256 Bytes PARMS Byte Array that is a Total of 1080Bytes UNUSED Byte Array that is a Total of 708Bytes This this what I want? <StructLayout(LayoutKind.Explicit, CharSet:=CharSet.Ansi, Pack:=1,
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.