for...in

  • Revision slug: Talk:JavaScript/Reference/Statements/for...in
  • Revision title: for...in
  • Revision id: 126698
  • Created:
  • Creator: Waldo
  • Is current revision? No
  • Comment /* mutating the array */ followup

Revision Content

There is a <tt>for each (variable in list)</tt> statement in Gecko 1.8a2. It is meant for E4X compatibility but seems to be available for all types of lists. Maybe this should be documented? --Wladimir Palant 08:00, 21 Jul 2005 (PDT)

Breadcrumbs

As of this writing the breadcrumbs output on this article is wrong. I filed {{template.Bug(326770)}} regarding the issue. --Callek 20:33, 10 February 2006 (PST)

sorted enumeration

ECMA 262 says nothing about the order of <tt>for .. in</tt>'s enumerated attributes being lexicographically sorted/ordered, but all versions of JS appear to do this sorting. I've seen code that relies on it. Worth a documentation note?

note on use with arrays

Callek writes:

In my opinion, the Description covers the warning expressed in the added text.

It does, but using for...in with arrays, and expecting it to return purely numeric indexes, is a common newbie mistake, and in my opinion warranted the more verbose note. So I'm restoring the note removed in the 11 February 2006 revision. --Tmetro 10:40, 20 September 2006 (PDT)

mutating the array

It might be worth a note that mutating the array (using Array.splice in my case) would exhibit the same behaviour one would see with a manual for loop - some elements get skipped / repeated / etc. Not going to do that myself with the recent posts in m.d.mdc about accidentally changing meaning though! :) --Mook 01:10, 24 December 2006 (PST)

_Please_ make the change yourself. You can try getting a review from waldo or someone afterwards. If you don't make it, there's a high chance that no-one will. --Nickolay 07:35, 24 December 2006 (PST)
 : I just made some changes which should hopefully address the mutation aspect. Feel free to say something here if you see improvements to my wording; I'm sure there are several which could be made, but I was trying not to be overly verbose. (Regarding hesitancy to make changes, I echo Nickolay -- make the change and post a request for review to the newsgroup, please.  :-) ) --Waldo 19:46, 6 January 2007 (PST)

Revision Source

<p>There is a <tt>for each (<var>variable</var> in <var>list</var>)</tt> statement in Gecko 1.8a2. It is meant for E4X compatibility but seems to be available for all types of lists. Maybe this should be documented?
--<a href="User:Wladimir_Palant">Wladimir Palant</a> 08:00, 21 Jul 2005 (PDT)
</p>
<h4 name="Breadcrumbs"> Breadcrumbs </h4>
<p>As of this writing the breadcrumbs output on this article is wrong.  I filed {{template.Bug(326770)}} regarding the issue. --<a href="User:Callek">Callek</a> 20:33, 10 February 2006 (PST)
</p>
<h4 name="sorted_enumeration"> sorted enumeration </h4>
<p>ECMA 262 says nothing about the order of <tt>for .. in</tt>'s enumerated attributes being lexicographically sorted/ordered, but all versions of JS appear to do this sorting. I've seen code that relies on it. Worth a documentation note?
</p>
<h4 name="note_on_use_with_arrays"> note on use with arrays </h4>
<p>Callek writes: 
</p>
<blockquote>
In my opinion, the Description covers the warning expressed in the added text.
</blockquote>
<p>It does, but using <b>for...in</b> with arrays, and expecting it to return purely numeric indexes, is a common newbie mistake, and in my opinion warranted the more verbose note. So I'm restoring the note removed in the <a class="external" href="http://developer.mozilla.org/en/docs/index.php?title=Core_JavaScript_1.5_Reference:Statements:for...in&amp;oldid=24734">11 February 2006</a> revision.
--<a href="User:Tmetro">Tmetro</a> 10:40, 20 September 2006 (PDT)
</p>
<h4 name="mutating_the_array"> mutating the array </h4>
<p>It might be worth a note that mutating the array (using <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/splice">Array.splice</a> in my case) would exhibit the same behaviour one would see with a manual <a href="en/Core_JavaScript_1.5_Reference/Statements/for">for</a> loop - some elements get skipped / repeated / etc.  Not going to do that myself with the recent posts in m.d.mdc about accidentally changing meaning though! :)
--<a href="User:Mook">Mook</a> 01:10, 24 December 2006 (PST)
</p>
<dl><dd> _Please_ make the change yourself. You can try getting a review from waldo or someone afterwards. If you don't make it, there's a high chance that no-one will. --<a href="User:Nickolay">Nickolay</a> 07:35, 24 December 2006 (PST)
</dd></dl>
<dl><dd> : I just made some changes which should hopefully address the mutation aspect.  Feel free to say something here if you see improvements to my wording; I'm sure there are several which could be made, but I was trying not to be overly verbose. (Regarding hesitancy to make changes, I echo Nickolay -- make the change and post a request for review to the newsgroup, please.  :-) ) --<a href="User:Waldo">Waldo</a> 19:46, 6 January 2007 (PST)
</dd></dl>
Revert to this revision