<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Iteration in JavaScript</title>
	<atom:link href="http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/</link>
	<description>Bob's Rants</description>
	<pubDate>Thu, 24 Jul 2008 01:22:32 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: bob</title>
		<link>http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/#comment-4066</link>
		<dc:creator>bob</dc:creator>
		<pubDate>Wed, 20 Dec 2006 02:23:31 +0000</pubDate>
		<guid isPermaLink="false">/?p=152#comment-4066</guid>
		<description>++i isn't meaningfully faster than i++ in most JS interpreters.

for each (n in nodes) is faster than for (i=0; ...) though.</description>
		<content:encoded><![CDATA[<p>++i isn&#8217;t meaningfully faster than i++ in most JS interpreters.</p>
<p>for each (n in nodes) is faster than for (i=0; &#8230;) though.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: NS</title>
		<link>http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/#comment-4063</link>
		<dc:creator>NS</dc:creator>
		<pubDate>Tue, 19 Dec 2006 18:27:08 +0000</pubDate>
		<guid isPermaLink="false">/?p=152#comment-4063</guid>
		<description>Because ++i is faster.  You seem concerned with having perfect code, and one should never use i++ unless it's necessary.


I ask about the 1.7 iterators because I was curious if they captured all of what you are trying to do.


Do you know if it is possible with JS 1.7 to, for instance, apply a forEach() method to an HTML Collection object?  This is the best I could come up with:

  var nodes = Iterator(document.getElementsByTagName("input"))
  for ( n in nodes )
    if ( n[1].getAttribute("type") == "text" )
      n[1].value = null

But that doesn't seem any different than:

  var nodes = document.getElementsByTagName("input")
  for ( let i = 0; i </description>
		<content:encoded><![CDATA[<p>Because ++i is faster.  You seem concerned with having perfect code, and one should never use i++ unless it&#8217;s necessary.</p>
<p>I ask about the 1.7 iterators because I was curious if they captured all of what you are trying to do.</p>
<p>Do you know if it is possible with JS 1.7 to, for instance, apply a forEach() method to an HTML Collection object?  This is the best I could come up with:</p>
<p>  var nodes = Iterator(document.getElementsByTagName(&#8221;input&#8221;))<br />
  for ( n in nodes )<br />
    if ( n[1].getAttribute(&#8221;type&#8221;) == &#8220;text&#8221; )<br />
      n[1].value = null</p>
<p>But that doesn&#8217;t seem any different than:</p>
<p>  var nodes = document.getElementsByTagName(&#8221;input&#8221;)<br />
  for ( let i = 0; i</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bob</title>
		<link>http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/#comment-4062</link>
		<dc:creator>bob</dc:creator>
		<pubDate>Tue, 19 Dec 2006 17:06:02 +0000</pubDate>
		<guid isPermaLink="false">/?p=152#comment-4062</guid>
		<description>There's absolutely no difference whatsoever between ++i and i++ in this context. I don't know why you wasted the effort to suggest one over the other.

JavaScript 1.7's iterators *are* what I proposed here, plus other enhancements. They effectively tracked Python 2.5's iterators.</description>
		<content:encoded><![CDATA[<p>There&#8217;s absolutely no difference whatsoever between ++i and i++ in this context. I don&#8217;t know why you wasted the effort to suggest one over the other.</p>
<p>JavaScript 1.7&#8217;s iterators *are* what I proposed here, plus other enhancements. They effectively tracked Python 2.5&#8217;s iterators.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: NS</title>
		<link>http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/#comment-4061</link>
		<dc:creator>NS</dc:creator>
		<pubDate>Tue, 19 Dec 2006 16:48:14 +0000</pubDate>
		<guid isPermaLink="false">/?p=152#comment-4061</guid>
		<description>First, regarding for loops and properly using them... ++i, not i++.

Second, now that Javascript 1.7 is available with built in Python-esque iterators, how does that compare with what you were trying to achieve?</description>
		<content:encoded><![CDATA[<p>First, regarding for loops and properly using them&#8230; ++i, not i++.</p>
<p>Second, now that Javascript 1.7 is available with built in Python-esque iterators, how does that compare with what you were trying to achieve?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob Ippolito</title>
		<link>http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/#comment-2527</link>
		<dc:creator>Bob Ippolito</dc:creator>
		<pubDate>Fri, 05 Aug 2005 23:47:48 +0000</pubDate>
		<guid isPermaLink="false">/?p=152#comment-2527</guid>
		<description>Actually, no, that won't work unless the array contains only strings.  Property names are strings, not arbitrary objects (though some implementations support that).</description>
		<content:encoded><![CDATA[<p>Actually, no, that won&#8217;t work unless the array contains only strings.  Property names are strings, not arbitrary objects (though some implementations support that).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Per Vognsen</title>
		<link>http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/#comment-2501</link>
		<dc:creator>Per Vognsen</dc:creator>
		<pubDate>Fri, 05 Aug 2005 15:29:36 +0000</pubDate>
		<guid isPermaLink="false">/?p=152#comment-2501</guid>
		<description>Just thought I'd respond to this:

&lt;em&gt;Property enumeration is only really useful for debugging, since chances are the objects will have a lot of properties that you do not want them to have, and there is no standard way to have properties that are hidden from enumeration.&lt;/em&gt;

This is a gross exageration. There's an easy way to transform an array into an associative array such that you can iterate over it using the 'in' syntax. A simple example:

&lt;code&gt;
function arrayIter(array)
{
    var obj = {};
    for (var i = 0; i &#60; array.length; i++)
    {
        obj[array[i]] = true;
    }
    return obj;
}

var myArray = [4,9,16];

for (elem in arrayIter(myArray))
{
    document.writeln(elem);
}
&lt;/code&gt;
Admittedly I don't see a way of having 'in' work together with arbitrary iterators, representing infinite streams or the like.</description>
		<content:encoded><![CDATA[<p>Just thought I&#8217;d respond to this:</p>
<p><em>Property enumeration is only really useful for debugging, since chances are the objects will have a lot of properties that you do not want them to have, and there is no standard way to have properties that are hidden from enumeration.</em></p>
<p>This is a gross exageration. There&#8217;s an easy way to transform an array into an associative array such that you can iterate over it using the &#8216;in&#8217; syntax. A simple example:</p>
<p><code><br />
function arrayIter(array)<br />
{<br />
    var obj = {};<br />
    for (var i = 0; i &lt; array.length; i++)<br />
    {<br />
        obj[array[i]] = true;<br />
    }<br />
    return obj;<br />
}</p>
<p>var myArray = [4,9,16];</p>
<p>for (elem in arrayIter(myArray))<br />
{<br />
    document.writeln(elem);<br />
}<br />
</code><br />
Admittedly I don&#8217;t see a way of having &#8216;in&#8217; work together with arbitrary iterators, representing infinite streams or the like.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian Bicking</title>
		<link>http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/#comment-2467</link>
		<dc:creator>Ian Bicking</dc:creator>
		<pubDate>Thu, 28 Jul 2005 18:09:27 +0000</pubDate>
		<guid isPermaLink="false">/?p=152#comment-2467</guid>
		<description>Whoops, &lt;code&gt;makeMakeSentinal()&lt;/code&gt; should return &lt;code&gt;create&lt;/code&gt;.</description>
		<content:encoded><![CDATA[<p>Whoops, <code>makeMakeSentinal()</code> should return <code>create</code>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian Bicking</title>
		<link>http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/#comment-2466</link>
		<dc:creator>Ian Bicking</dc:creator>
		<pubDate>Thu, 28 Jul 2005 18:08:14 +0000</pubDate>
		<guid isPermaLink="false">/?p=152#comment-2466</guid>
		<description>I think that for loop would at least be better, if not great, if you did:

&lt;code&gt;for (var i=0, obj; ! (obj=all[i]) === undefined; i++) {}&lt;/code&gt;

In theory &lt;code&gt;undefined&lt;/code&gt; is a more purely sentinal object than &lt;code&gt;null&lt;/code&gt; or the other false objects.

If you wanted to make a purely sentinal object that would be less likely to leak into the environment, you could probably do:

&lt;code&gt;
function makeMakeSentinal() {&lt;br&gt;
&#160;  var sentinal = new Object();&lt;br&gt;
&#160;  function create() {&lt;br&gt;
&#160; &#160;   return sentinal;&lt;br&gt;
&#160;  }&lt;br&gt;
}&lt;br&gt;
makeSentinal = makeMakeSentinal();
&lt;/code&gt;

This would give you a truly private object with a unique and persistent identity that could only be retrieved with a function call, and so would never appear in any enumeration of an object (so long as people are disciplined enough not to store the result of that function call anywhere except in private local variables).

But the only justification I see for one over the other is performance, which requires testing, which I am too lazy to do now, so I can't complain...</description>
		<content:encoded><![CDATA[<p>I think that for loop would at least be better, if not great, if you did:</p>
<p><code>for (var i=0, obj; ! (obj=all[i]) === undefined; i++) {}</code></p>
<p>In theory <code>undefined</code> is a more purely sentinal object than <code>null</code> or the other false objects.</p>
<p>If you wanted to make a purely sentinal object that would be less likely to leak into the environment, you could probably do:</p>
<p><code><br />
function makeMakeSentinal() {<br />
&nbsp;  var sentinal = new Object();<br />
&nbsp;  function create() {<br />
&nbsp; &nbsp;   return sentinal;<br />
&nbsp;  }<br />
}<br />
makeSentinal = makeMakeSentinal();<br />
</code></p>
<p>This would give you a truly private object with a unique and persistent identity that could only be retrieved with a function call, and so would never appear in any enumeration of an object (so long as people are disciplined enough not to store the result of that function call anywhere except in private local variables).</p>
<p>But the only justification I see for one over the other is performance, which requires testing, which I am too lazy to do now, so I can&#8217;t complain&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob Ippolito</title>
		<link>http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/#comment-2453</link>
		<dc:creator>Bob Ippolito</dc:creator>
		<pubDate>Tue, 26 Jul 2005 04:59:42 +0000</pubDate>
		<guid isPermaLink="false">/?p=152#comment-2453</guid>
		<description>Uh.. no.  If all[i] happens to be a false value then you lose.</description>
		<content:encoded><![CDATA[<p>Uh.. no.  If all[i] happens to be a false value then you lose.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tim baker</title>
		<link>http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/#comment-2451</link>
		<dc:creator>tim baker</dc:creator>
		<pubDate>Tue, 26 Jul 2005 04:45:22 +0000</pubDate>
		<guid isPermaLink="false">/?p=152#comment-2451</guid>
		<description>oops, did not encode that "&#60;". i meant

&lt;code&gt;for (var i = 0, obj; obj = all[i]; i++) { ...&lt;/code&gt;

might be better than

&lt;code&gt;for (var i = 0; i &#60; all.length; i++) { var obj = all[i]; ...&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>oops, did not encode that &#8220;&lt;&#8221;. i meant</p>
<p><code>for (var i = 0, obj; obj = all[i]; i++) { &#8230;</code></p>
<p>might be better than</p>
<p><code>for (var i = 0; i &lt; all.length; i++) { var obj = all[i]; &#8230;</code></p>
]]></content:encoded>
	</item>
</channel>
</rss>
