<?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"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: How To Emulate a Foreach Loop in JavaScript</title> <atom:link href="http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/feed/" rel="self" type="application/rss+xml" /><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/</link> <description>Web Development Tutorials, Tips &#38; Techniques</description> <lastBuildDate>Tue, 07 Feb 2012 11:23:26 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>By: Joel Selvadurai</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-13649</link> <dc:creator>Joel Selvadurai</dc:creator> <pubDate>Wed, 02 Nov 2011 19:52:55 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-13649</guid> <description>Much better to use:for(var i in array) {
array[i] = &#039;whatever&#039;;
}</description> <content:encoded><![CDATA[<p>Much better to use:</p><p>for(var i in array) {</p><p> array[i] = &#8216;whatever&#8217;;</p><p> }</p> ]]></content:encoded> </item> <item><title>By: gman</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-13187</link> <dc:creator>gman</dc:creator> <pubDate>Tue, 11 Jan 2011 22:59:39 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-13187</guid> <description>Maybe the wrong place to put this but in answer to Chris&#039;s questionThe difference between i++ and ++i is as follows.The meaning of i++ isvar temp = i; i = i + 1; return temp;the meaning of ++i isi = i + 1; return i;It&#039;s the creation of that temp that *may* take more time. In JavaScript I suspect most modern browsers can optimize away the need for temp unless you are using it. But ++ comes from C/C++ and in C++ the creation of temp might be some complex type and have side effects.So, best to just get in the habit of coding what you mean. If you want to increment i then use ++i. If you want to both increment i and get the result before you incremented it than use i++.</description> <content:encoded><![CDATA[<p>Maybe the wrong place to put this but in answer to Chris&#8217;s question</p><p>The difference between i++ and ++i is as follows.</p><p>The meaning of i++ is</p><p> var temp = i; i = i + 1; return temp;</p><p>the meaning of ++i is</p><p> i = i + 1; return i;</p><p>It&#8217;s the creation of that temp that *may* take more time. In JavaScript I suspect most modern browsers can optimize away the need for temp unless you are using it. But ++ comes from C/C++ and in C++ the creation of temp might be some complex type and have side effects.</p><p>So, best to just get in the habit of coding what you mean. If you want to increment i then use ++i. If you want to both increment i and get the result before you incremented it than use i++.</p> ]]></content:encoded> </item> <item><title>By: Saint John Web Design &#124; Informative Computer Solutions &#187; Blog Archive &#187; Build a Bare-Bones WordPress 2.8+ Widget</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-11199</link> <dc:creator>Saint John Web Design &#124; Informative Computer Solutions &#187; Blog Archive &#187; Build a Bare-Bones WordPress 2.8+ Widget</dc:creator> <pubDate>Sun, 07 Feb 2010 07:14:12 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-11199</guid> <description>[...] How To Emulate a Foreach Loop in JavaScript [...]</description> <content:encoded><![CDATA[<p>[...] How To Emulate a Foreach Loop in JavaScript [...]</p> ]]></content:encoded> </item> <item><title>By: Saint John Web Design &#124; Informative Computer Solutions &#187; Blog Archive &#187; How To Create Simple, Stylish and Swappable Image Captions</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-11196</link> <dc:creator>Saint John Web Design &#124; Informative Computer Solutions &#187; Blog Archive &#187; How To Create Simple, Stylish and Swappable Image Captions</dc:creator> <pubDate>Sun, 07 Feb 2010 04:32:15 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-11196</guid> <description>[...] How To Emulate a Foreach Loop in JavaScript [...]</description> <content:encoded><![CDATA[<p>[...] How To Emulate a Foreach Loop in JavaScript [...]</p> ]]></content:encoded> </item> <item><title>By: Saint John Web Design &#124; Informative Computer Solutions &#187; Blog Archive &#187; Running jQuery with Other Frameworks Via noConflict</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-11190</link> <dc:creator>Saint John Web Design &#124; Informative Computer Solutions &#187; Blog Archive &#187; Running jQuery with Other Frameworks Via noConflict</dc:creator> <pubDate>Sun, 07 Feb 2010 04:27:44 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-11190</guid> <description>[...] How To Emulate a Foreach Loop in JavaScript [...]</description> <content:encoded><![CDATA[<p>[...] How To Emulate a Foreach Loop in JavaScript [...]</p> ]]></content:encoded> </item> <item><title>By: Mike Capson &#187; Blog Archive &#187; How To Create Simple, Stylish and Swappable Image Captions</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-11181</link> <dc:creator>Mike Capson &#187; Blog Archive &#187; How To Create Simple, Stylish and Swappable Image Captions</dc:creator> <pubDate>Sat, 06 Feb 2010 00:59:47 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-11181</guid> <description>[...] How To Emulate a Foreach Loop in JavaScript [...]</description> <content:encoded><![CDATA[<p>[...] How To Emulate a Foreach Loop in JavaScript [...]</p> ]]></content:encoded> </item> <item><title>By: Lee Kowalkowski</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-11113</link> <dc:creator>Lee Kowalkowski</dc:creator> <pubDate>Wed, 03 Feb 2010 13:29:47 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-11113</guid> <description>You only need to cache the array length if the array is actually a DOM collection (e.g. element.childNodes).  JS for(... in ...) won&#039;t work cross browser over DOM collections anyway - because some browsers include the length property and will use that as one of the iterations.You need for(... in ...) to iterate through properties of an object though, therefore I&#039;d never advise modifying Array.prototype.  E.g:var a = [];
a[&quot;key1&quot;] = &quot;value1&quot;;
a[&quot;key2&quot;] = &quot;value2&quot;;.. if Array.prototype has been modified, it makes things more difficult.</description> <content:encoded><![CDATA[<p>You only need to cache the array length if the array is actually a DOM collection (e.g. element.childNodes).  JS for(&#8230; in &#8230;) won&#8217;t work cross browser over DOM collections anyway &#8211; because some browsers include the length property and will use that as one of the iterations.</p><p>You need for(&#8230; in &#8230;) to iterate through properties of an object though, therefore I&#8217;d never advise modifying Array.prototype.  E.g:</p><p>var a = [];<br
/> a["key1"] = &#8220;value1&#8243;;<br
/> a["key2"] = &#8220;value2&#8243;;</p><p>.. if Array.prototype has been modified, it makes things more difficult.</p> ]]></content:encoded> </item> <item><title>By: Rob Glazebrook</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-10762</link> <dc:creator>Rob Glazebrook</dc:creator> <pubDate>Mon, 25 Jan 2010 15:27:15 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-10762</guid> <description>Hi Frank,My only concern with Brandon&#039;s example is that if you ~do~ happen to be using a library like jQuery (and that&#039;s a growing percentage of the web populace), that structure can give you problems. I tested it for the purposes of this article and array[i] was returning a function, not the array string I was expecting.So, if you&#039;re not using jQuery or a similar framework, and never plan to use a JavaScript framework on your pages, by all means go with that system. But if you&#039;re already using jQuery or another framework for something else, you may run into problems!</description> <content:encoded><![CDATA[<p>Hi Frank,</p><p>My only concern with Brandon&#8217;s example is that if you ~do~ happen to be using a library like jQuery (and that&#8217;s a growing percentage of the web populace), that structure can give you problems. I tested it for the purposes of this article and array[i] was returning a function, not the array string I was expecting.</p><p>So, if you&#8217;re not using jQuery or a similar framework, and never plan to use a JavaScript framework on your pages, by all means go with that system. But if you&#8217;re already using jQuery or another framework for something else, you may run into problems!</p> ]]></content:encoded> </item> <item><title>By: Frank Schenk</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-10706</link> <dc:creator>Frank Schenk</dc:creator> <pubDate>Sun, 24 Jan 2010 20:09:41 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-10706</guid> <description>What&#039;s wrong with Brandons example? It works perfectly and it doesn&#039;t require a bloated library like jquery.for() is btw much faster than foreach in php, if the maximum number is given as variable, not as function, thats the problem with foreach, as it always has to check the content of the given array.so:
$max = count( $array);
for( $i=0; $&lt;$max; $i++){
$array[$i] = &#039;Do whatever you want to do&#039;;
}is way faster than foreach.cheers, Frank</description> <content:encoded><![CDATA[<p>What&#8217;s wrong with Brandons example? It works perfectly and it doesn&#8217;t require a bloated library like jquery.</p><p>for() is btw much faster than foreach in php, if the maximum number is given as variable, not as function, thats the problem with foreach, as it always has to check the content of the given array.</p><p>so:<br
/> $max = count( $array);<br
/> for( $i=0; $&lt;$max; $i++){<br
/> $array[$i] = &#039;Do whatever you want to do&#039;;<br
/> }</p><p>is way faster than foreach.</p><p>cheers, Frank</p> ]]></content:encoded> </item> <item><title>By: mike stevens</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-10705</link> <dc:creator>mike stevens</dc:creator> <pubDate>Sun, 24 Jan 2010 19:58:17 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-10705</guid> <description>Very nice article.  The cross language examples are insightful</description> <content:encoded><![CDATA[<p>Very nice article.  The cross language examples are insightful</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)

Served from: www.cssnewbie.com @ 2012-02-08 03:38:07 -->
