By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,935 Members | 1,528 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,935 IT Pros & Developers. It's quick & easy.

Q: JScript associative array

P: n/a
Platform: Win2K/SP4

I am trying to use an associative array in JScript, but I am seeing
something weird. Example:

ArrayTest();

///////////////////////////////////////////////////
function ArrayTest()
{
var oArray = new Array( 0 );
dumpArrayKeys( oArray );

++oArray.length;
oArray["first"] = 0;
dumpArrayKeys( oArray );

++oArray.length;
oArray["second"] = 1;
dumpArrayKeys( oArray );
}
///////////////////////////////////////////////////
function dumpArrayKeys( oArray )
{
oLog.Log( "arrayLen: " + oArray.length );
for( var key in oArray )
{
oLog.Log( "key: " + key );
}
Log( "-----------------------------" );
}
///////////////////////////////////////////////////

The output I'm seeing:

arrayLen: 0
key: getKey
-----------------------------
arrayLen: 1
key: getKey
key: first
-----------------------------
arrayLen: 2
key: getKey
key: first
key: second
-----------------------------

I don't understand where the key value "getKey" is coming from or
what it represents. Notice that the key is there as soon as the Array
has been created, and it never goes away.

Thoughts?

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> I am trying to use an associative array in JScript, but I am seeing
something weird.


We see stuff like this all the time. In JavaScript, do not use an array
when you should be using an object. The rule is really simple:

If the subscripts are small integers, use an array.
Otherwise, use an object.

See http://www.crockford.com/javascript/survey.html
Jul 23 '05 #2

P: n/a
On 25 Jan 2005 18:19:14 -0800, bu********@yahoo.com wrote:
Platform: Win2K/SP4

I am trying to use an associative array in JScript, but I am seeing
something weird. Example:

ArrayTest();

///////////////////////////////////////////////////
function ArrayTest()
{
var oArray = new Array( 0 );
dumpArrayKeys( oArray );

++oArray.length;
oArray["first"] = 0;
dumpArrayKeys( oArray );

++oArray.length;
oArray["second"] = 1;
dumpArrayKeys( oArray );
}
///////////////////////////////////////////////////
function dumpArrayKeys( oArray )
{
oLog.Log( "arrayLen: " + oArray.length );
for( var key in oArray )
{
oLog.Log( "key: " + key );
}
Log( "-----------------------------" );
}
///////////////////////////////////////////////////

The output I'm seeing:

arrayLen: 0
key: getKey
-----------------------------
arrayLen: 1
key: getKey
key: first
-----------------------------
arrayLen: 2
key: getKey
key: first
key: second
-----------------------------

I don't understand where the key value "getKey" is coming from or
what it represents. Notice that the key is there as soon as the Array
has been created, and it never goes away.

Thoughts?


That code (once the errors are fixed) doesn't result in that output.
You've probably got more code that you didn't post where you added a
getKey method to the Array prototype, e.g.
Array.prototype.getKey = something;

Regards,
Steve
--
Please post questions to the newsgroup; everyone benefits.
This posting is provided "AS IS" with no warranties, and confers no rights.
Sample code subject to http://www.microsoft.com/info/cpyright.htm
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.