Understanding Arrays and enumeration

Discussion in 'Javascript' started by Christopher T King, Aug 13, 2004.

  1. Reading through the ECMAScript spec, it seems that this code:

    o = Object()
    o['a'] = 5
    o['b'] = 6
    o['c'] = 7
    for (var i in o) print(i);

    should result in 'a', 'b', and 'c' being printed (not necessarily in that
    order), and this is indeed the outcome. However, it also seems that this
    code:

    o = Array()
    o[0] = 5
    o[1] = 6
    o[2] = 7
    for (var i in o) print(i);

    should result in 0, 1, and 2 being printed. However, this is not the
    case: 5, 6, and 7 are instead printed.

    I know these results are intuitively correct, but I can't find anything in
    the ECMA spec making this distinction for Arrays. The for (i in o)
    construction is defined to enumerate the properties of o, not the values
    of those properties, and an array assignment o[n] = x is defined to assign
    the value x to the property n of the object o.

    Am I missing something in the spec, is the implementation I'm using (njs)
    in error, or is this a discrepancy between JavaScript and ECMAScript?

    Thanks in advance.
    Christopher T King, Aug 13, 2004
    #1
    1. Advertising

  2. Christopher T King

    G Roydor Guest

    print(o);

    GR

    Christopher T King a écrit:
    > Reading through the ECMAScript spec, it seems that this code:
    >
    > o = Object()
    > o['a'] = 5
    > o['b'] = 6
    > o['c'] = 7
    > for (var i in o) print(i);
    >
    > should result in 'a', 'b', and 'c' being printed (not necessarily in that
    > order), and this is indeed the outcome. However, it also seems that this
    > code:
    >
    > o = Array()
    > o[0] = 5
    > o[1] = 6
    > o[2] = 7
    > for (var i in o) print(i);
    >
    > should result in 0, 1, and 2 being printed. However, this is not the
    > case: 5, 6, and 7 are instead printed.
    >
    > I know these results are intuitively correct, but I can't find anything in
    > the ECMA spec making this distinction for Arrays. The for (i in o)
    > construction is defined to enumerate the properties of o, not the values
    > of those properties, and an array assignment o[n] = x is defined to assign
    > the value x to the property n of the object o.
    >
    > Am I missing something in the spec, is the implementation I'm using (njs)
    > in error, or is this a discrepancy between JavaScript and ECMAScript?
    >
    > Thanks in advance.
    >
    G Roydor, Aug 13, 2004
    #2
    1. Advertising

  3. Christopher T King <> writes:

    ....
    > o = Array()
    > o[0] = 5
    > o[1] = 6
    > o[2] = 7
    > for (var i in o) print(i);
    >
    > should result in 0, 1, and 2 being printed. However, this is not the
    > case: 5, 6, and 7 are instead printed.


    Testing in a browser (changing "print" to "alert", since "print" will
    print the page) gives me 0, 1 and 2.

    What are you using for executing the code (browser/non-browser
    environment)? Are you *sure* that is the code you are running?

    > Am I missing something in the spec, is the implementation I'm using (njs)
    > in error, or is this a discrepancy between JavaScript and ECMAScript?


    JavaScript (the scripting language in Netscape browsers) and JScript
    (the scripting language in IE) are both ECMAScript compatible, and
    have been the last several versions.


    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Aug 14, 2004
    #3
  4. On Sat, 14 Aug 2004, Lasse Reichstein Nielsen wrote:

    > Testing in a browser (changing "print" to "alert", since "print" will
    > print the page) gives me 0, 1 and 2.


    Okay, doing the same thing gets the same results for me too. It would
    seem the interpreter I'm using is incorrect, then.

    > What are you using for executing the code (browser/non-browser
    > environment)? Are you *sure* that is the code you are running?


    I'm using njs (http://www.njs-javascript.org/). It's a standalone
    interpreter designed for embedding in other applications.

    > JavaScript (the scripting language in Netscape browsers) and JScript
    > (the scripting language in IE) are both ECMAScript compatible, and
    > have been the last several versions.


    But it seems that njs is not, at least in this area ;)

    Thanks a lot!
    Christopher T King, Aug 14, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dj

    help understanding character arrays

    dj, May 19, 2004, in forum: C Programming
    Replies:
    3
    Views:
    340
    Barry Schwarz
    May 23, 2004
  2. venkatagmail
    Replies:
    11
    Views:
    657
    James Kanze
    Oct 3, 2007
  3. puvit82
    Replies:
    4
    Views:
    737
    puvit82
    Feb 1, 2008
  4. Philipp
    Replies:
    21
    Views:
    1,093
    Philipp
    Jan 20, 2009
  5. MG
    Replies:
    5
    Views:
    176
    Thomas 'PointedEars' Lahn
    Apr 16, 2010
Loading...

Share This Page