Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Understanding Arrays and enumeration

Reply
Thread Tools

Understanding Arrays and enumeration

 
 
Christopher T King
Guest
Posts: n/a
 
      08-13-2004
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.

 
Reply With Quote
 
 
 
 
G Roydor
Guest
Posts: n/a
 
      08-13-2004


print(o[i]);

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.
>


 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      08-13-2004
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.'
 
Reply With Quote
 
Christopher T King
Guest
Posts: n/a
 
      08-14-2004
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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Understanding Arrays within Arrays MG Javascript 5 04-16-2010 07:57 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Re-using a simple type definition; with enumeration constraint andwithout enumeration constraint puvit82 XML 4 02-01-2008 03:46 PM
Problem understanding pass by value and pass by reference of arrays and what happens in the memory venkatagmail C++ 11 10-03-2007 02:00 PM
help understanding character arrays dj C Programming 3 05-23-2004 03:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57