Q: JScript associative array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bubbabubbs@yahoo.com

    #1

    Q: JScript associative array

    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?

  • Douglas Crockford

    #2
    Re: Q: JScript associative array

    > I am trying to use an associative array in JScript, but I am seeing[color=blue]
    > something weird.[/color]

    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

    Comment

    • Steve van Dongen [MSFT]

      #3
      Re: Q: JScript associative array

      On 25 Jan 2005 18:19:14 -0800, bubbabubbs@yaho o.com wrote:
      [color=blue]
      >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 );
      >dumpArrayKey s( oArray );
      >
      >++oArray.lengt h;
      >oArray["first"] = 0;
      >dumpArrayKey s( oArray );
      >
      >++oArray.lengt h;
      >oArray["second"] = 1;
      >dumpArrayKey s( 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?[/color]

      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

      Comment

      Working...