for...in

  • Revision slug: Talk:JavaScript/Reference/Statements/for...in
  • Revision title: for...in
  • Revision id: 287302
  • Created:
  • Creator: oberhamsi
  • Is current revision? Yes
  • Comment adds sig; 1 words added

Revision Content

There is a for each (variable in list) 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)

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

sorted enumeration

ECMA 262 says nothing about the order of for .. in'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)

'never use on arrays'

i assume this warning is obsolete now that with 1.7 array comprehension is actually encouraged, as seen here https://developer.mozilla.org/en/New_in_javascript_1.7#Array_comprehensions. i suggest just leaving this part:

The for...in statement iterates over user-defined properties in addition to the array elements, so if you modify the array's non-integer or non-positive properties (e.g. by adding a "foo" property to it or even by adding a method or property to Array.prototype), the for...in statement will return the name of your user-defined properties in addition to the numeric indexes.

similarly i would remove the Warning here https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/for...in#Description

oberhamsi

Revision Source

<p>There is a <code>for each (<var>variable</var> in <var>list</var>)</code> 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" title="User:Wladimir_Palant">Wladimir Palant</a> 08:00, 21 Jul 2005 (PDT)</p>
<h4 id="Breadcrumbs" name="Breadcrumbs">Breadcrumbs</h4>
<p>As of this writing the breadcrumbs output on this article is wrong. I filed {{ Bug(326770) }} regarding the issue. --<a href="/User:Callek" title="User:Callek">Callek</a> 20:33, 10 February 2006 (PST)</p>
<h4 id="sorted_enumeration" name="sorted_enumeration">sorted enumeration</h4>
<p>ECMA 262 says nothing about the order of <code>for .. in</code>'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 id="note_on_use_with_arrays" 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 <strong>for...in</strong> 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" title="User:Tmetro">Tmetro</a> 10:40, 20 September 2006 (PDT)</p>
<h4 id="mutating_the_array" 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" title="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" title="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" title="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" title="User:Nickolay">Nickolay</a> 07:35, 24 December 2006 (PST)</dd>
</dl>
<dl> <dd> <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" title="User:Waldo">Waldo</a> 19:46, 6 January 2007 (PST)</dd> </dl> </dd> <h4 id="'never_use_on_arrays'">'never use on arrays'</h4> <p>i assume this warning is obsolete now that with 1.7 array comprehension is actually encouraged, as seen here https://developer.mozilla.org/en/New_in_javascript_1.7#Array_comprehensions. i suggest just leaving this part:</p> <p style="margin-left: 40px;">The <code>for...in</code> statement iterates over user-defined properties in addition to the array elements, so if you modify the array's non-integer or non-positive properties (e.g. by adding a <code>"foo"</code> property to it or even by adding a method or property to <code>Array.prototype</code>), the <code>for...in</code> statement will return the name of your user-defined properties in addition to the numeric indexes.</p> <p>similarly i would remove the Warning here https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/for...in#Description</p> <p><a href="https://developer.mozilla.org/User:oberhamsi" rel="custom nofollow">oberhamsi</a></p>
</dl>
Revert to this revision