Understanding Arrays and enumeration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher T King

    #1

    Understanding Arrays and enumeration

    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

    #2
    Re: Understanding Arrays and enumeration



    print(o[i]);

    GR

    Christopher T King a écrit:[color=blue]
    > 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.
    >[/color]

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Understanding Arrays and enumeration

      Christopher T King <squirrel@WPI.E DU> writes:

      ....[color=blue]
      > 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.[/color]

      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?
      [color=blue]
      > 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?[/color]

      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 - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Christopher T King

        #4
        Re: Understanding Arrays and enumeration

        On Sat, 14 Aug 2004, Lasse Reichstein Nielsen wrote:
        [color=blue]
        > Testing in a browser (changing "print" to "alert", since "print" will
        > print the page) gives me 0, 1 and 2.[/color]

        Okay, doing the same thing gets the same results for me too. It would
        seem the interpreter I'm using is incorrect, then.
        [color=blue]
        > What are you using for executing the code (browser/non-browser
        > environment)? Are you *sure* that is the code you are running?[/color]

        I'm using njs (http://www.njs-javascript.org/). It's a standalone
        interpreter designed for embedding in other applications.
        [color=blue]
        > 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.[/color]

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

        Thanks a lot!

        Comment

        Working...