Q: JScript associative array

B

bubbabubbs

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?
 
S

Steve van Dongen [MSFT]

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
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top