Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / September 2005



Tip: Looking for answers? Try searching our database.

for/in loop Array gotcha

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ian Osgood - 29 Sep 2005 16:49 GMT
I just wanted to let folks know of one more reason not to use for/in
loops on Arrays.

In the following code:

var u = [1,1]
for (var i in u)
  print([i, i+2, i-0+2].join());
print(typeof(i) + ' ' + typeof(i-0));

 0,02,2
 1,12,3
 string number

(i+1) does not obtain the expected results because i is a string!
Always use C-like loops when iterating arrays.  JavaScript is not
Python!

Ian
pipe - 29 Sep 2005 17:02 GMT
var u = [1,1]
for (var i in u)
  print([i, 2+i, -0+2+parseInt(i)].join());
print(typeof(i) + ' ' + typeof(i-0));

output left for readers to guess
Michael Winter - 29 Sep 2005 17:20 GMT
[snip]

>  for (var i in u)

[snip]

> (i+1) does not obtain the expected results because i is a string!

Of course, because property names are /always/ strings.

[snip]

Mike

Signature

Michael Winter
Prefix subject with [News] before replying by e-mail.

aundro - 30 Sep 2005 14:44 GMT
> I just wanted to let folks know of one more reason not to use for/in
> loops on Arrays.
[quoted text clipped - 13 lines]
> Always use C-like loops when iterating arrays.  JavaScript is not
> Python!

Or, you could read the ecmascript spec, in order to find out what the
"for .. in" syntax actually does.

Your "advice" sounds more like a warning than anything else, but I'm
actually glad the "for .. in" syntax exists.

> Ian

     Arnaud
McKirahan - 30 Sep 2005 14:58 GMT
[snip]

> ... I'm actually glad the "for .. in" syntax exists.

I didn't know of it before; thanks.

"The for...in statement is used to iterate a declared variable over every
property in a specified object. The code in the body of the for ... in loop
is executed once for each property."

http://www.devguru.com/Technologies/ecmascript/quickref/for...in.html
Baconbutty - 30 Sep 2005 15:04 GMT
Note also the following odd usage to test for the existence of a
property:-

var a={};
a.myProp=1;

var exists=(myProp in a);
aundro - 30 Sep 2005 15:25 GMT
> Note also the following odd usage to test for the existence of a
> property:-
[quoted text clipped - 3 lines]
>
> var exists=(myProp in a);

Is that proper JS? When I execute in SpiderMonkey:

----8<----
aundro@paddy:~$ js
js> var a={};
js> a.myProp=1;
1
js> var exists=(myProp in a);
3: ReferenceError: myProp is not defined
----8<----

*sob*

Regards,
     Arnaud
Martin Honnen - 30 Sep 2005 15:25 GMT
> Note also the following odd usage to test for the existence of a
> property:-
[quoted text clipped - 3 lines]
>
> var exists=(myProp in a);

That snippet will give you an error, you need
  var exists = "myProp" in a;

There is nothing "odd" about that in my view only that the 'in' operator
is not implemented in older browsers respectively the script engines
they come with, e.g. in Netscape 4 the 'in' operator use will give a
syntax error, the same for IE 5/Mac if I remember a recent discussion
here correctly.

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

Baconbutty - 30 Sep 2005 17:12 GMT
>>That snippet will give you an error, you need
>>var exists = "myProp" in a;

Oops, thank you for that.
aundro - 30 Sep 2005 15:23 GMT
> [snip]
>
[quoted text clipped - 5 lines]
> property in a specified object. The code in the body of the for ... in loop
> is executed once for each property."

Ok, here comes a little pedantry:  

Some properties won't be iterated over (yet remain accessible) if they
have the 'DontEnum' attribute. How you can reach/set/clear those attributes
however, I absolutely have no idea.. Anybody has any idea?

(see P.65 of www.ecma-international.org/publications/standards/Ecma-262.htm)

Basically: you don't care about such properties, but heh, it's just
nice to know there can be 'hidden' properties.

> http://www.devguru.com/Technologies/ecmascript/quickref/for...in.html

       Arnaud
Martin Honnen - 30 Sep 2005 17:25 GMT
> Some properties won't be iterated over (yet remain accessible) if they
> have the 'DontEnum' attribute. How you can reach/set/clear those attributes
> however,

Script code itself can't read or set that attribute, it is internal to
an implementation and if you have an application hosting a script engine
then you usually have access to set such attributes on the properties of
the host objects the application creates.

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

Arnaud Diederen - 30 Sep 2005 18:51 GMT
>> Some properties won't be iterated over (yet remain accessible) if they
>> have the 'DontEnum' attribute. How you can reach/set/clear those attributes
[quoted text clipped - 4 lines]
> engine then you usually have access to set such attributes on the
> properties of the host objects the application creates.

That's indeed more or less what I was conceiving, but thanks a lot for
the clarification :)

         Arnaud
Lasse Reichstein Nielsen - 30 Sep 2005 18:57 GMT
[property attributes like DontEnum]
> Script code itself can't read or set that attribute,

Almost correct. There is Object.prototype.propertyIsEnumerable that
allows you to read one of the three attributes (the other two being
ReadOnly and DontDelete, which can't be read, but might be discoverable
by trying to do it and catching the exception :)

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Michael Winter - 30 Sep 2005 20:55 GMT
[snip]

> (the other two being ReadOnly and DontDelete, which can't be read,
> but might be discoverable by trying to do it and catching the
> exception :)

No exceptions should be thrown if the delete or assignment operators are
used on DontDelete or ReadOnly properties, respectively. The operation
should silently fail (though the delete operator should evaluate to false).

Mike

Signature

Michael Winter
Prefix subject with [News] before replying by e-mail.

Ian Osgood - 30 Sep 2005 17:10 GMT
> [snip]
>
[quoted text clipped - 7 lines]
>
> http://www.devguru.com/Technologies/ecmascript/quickref/for...in.html

Nice reference.  I note that this reference also mentions the use of
for..in on arrays, but without mentioning that the variable comes back
as a string instead of a number.

Ian
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.