<?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: A collection is not an array</title>
	<atom:link href="http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/</link>
	<description>News, opinion, and fresh thinking for web developers and designers. The official podcast of sitepoint.com.</description>
	<pubDate>Tue, 02 Dec 2008 04:38:58 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: brothercake</title>
		<link>http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-739164</link>
		<dc:creator>brothercake</dc:creator>
		<pubDate>Wed, 04 Jun 2008 08:49:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-739164</guid>
		<description>What can go wrong with the former is the appearance of external prototypes of built-in objects, such as the stuff that the prototype library does.  If you have something like this:

&lt;code&gt;Array.prototype.myCustomFunction = function()
{
    ....
}&lt;/code&gt;

Then a &lt;code&gt;for..in&lt;/code&gt; loop will include that method, because this loop iterates through all properties of an object, where a &lt;code&gt;for&lt;/code&gt; loop merely iterates through the numerically-indexed properties (the memebers of an array are merely a subset of its properties, namely, those properties that have a numeric index).

The easiest way around that is to use &lt;code&gt;hasOwnProperty&lt;/code&gt; to discriminate and ignore the properties you don't want:

&lt;code&gt;for(var i in obj)
{
  if(!obj.hasOwnProperty(i)) { continue; }

  //now we're okay
}&lt;/code&gt;

Is that the issue you were referring to?</description>
		<content:encoded><![CDATA[<p>What can go wrong with the former is the appearance of external prototypes of built-in objects, such as the stuff that the prototype library does.  If you have something like this:</p>
<code>Array.prototype.myCustomFunction = function()
{
    ....
}</code>
<p>Then a <code>for..in</code> loop will include that method, because this loop iterates through all properties of an object, where a <code>for</code> loop merely iterates through the numerically-indexed properties (the memebers of an array are merely a subset of its properties, namely, those properties that have a numeric index).</p>
<p>The easiest way around that is to use <code>hasOwnProperty</code> to discriminate and ignore the properties you don&#8217;t want:</p>
<code>for(var i in obj)
{
  if(!obj.hasOwnProperty(i)) { continue; }

  //now we're okay
}</code>
<p>Is that the issue you were referring to?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Meyer</title>
		<link>http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-736975</link>
		<dc:creator>Eric Meyer</dc:creator>
		<pubDate>Sun, 01 Jun 2008 02:35:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-736975</guid>
		<description>Is this why doing a for/in loop over a collection causes failures that don't happen with a straight for loop, and if so, can you explain exactly what goes wrong with the former that isn't triggered by the latter?</description>
		<content:encoded><![CDATA[<p>Is this why doing a for/in loop over a collection causes failures that don&#8217;t happen with a straight for loop, and if so, can you explain exactly what goes wrong with the former that isn&#8217;t triggered by the latter?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: brothercake</title>
		<link>http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-660636</link>
		<dc:creator>brothercake</dc:creator>
		<pubDate>Tue, 25 Mar 2008 23:09:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-660636</guid>
		<description>@cranial-bore: you can still bind events to those elements; how you store their references doesn't affect the elements themselves at all.

@Tino Zijdel: ah yeah, I'd forgotten about that, you can indeed effectively "steal" methods from Array.prototype. Nice :) Dan Webb talks about that in his metaprogramming chapter of Art &#38; Science of JS.

@kyberfabrikken: not sure what you mean there, can you elaborate?</description>
		<content:encoded><![CDATA[<p>@cranial-bore: you can still bind events to those elements; how you store their references doesn&#8217;t affect the elements themselves at all.</p>
<p>@Tino Zijdel: ah yeah, I&#8217;d forgotten about that, you can indeed effectively &#8220;steal&#8221; methods from Array.prototype. Nice :) Dan Webb talks about that in his metaprogramming chapter of Art &amp; Science of JS.</p>
<p>@kyberfabrikken: not sure what you mean there, can you elaborate?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: kyberfabrikken</title>
		<link>http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-660257</link>
		<dc:creator>kyberfabrikken</dc:creator>
		<pubDate>Tue, 25 Mar 2008 10:38:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-660257</guid>
		<description>&lt;blockquote&gt;A NodeList is a reference to a collection of objects&lt;/blockquote&gt;
This is actually a major gotcha -- NodeLists are late bound, which can cause quite some confusion, if you iterate over it and manipulate the DOM at the same time. For this reason alone, it's usually a good idea to convert it into an array.</description>
		<content:encoded><![CDATA[<blockquote><p>A NodeList is a reference to a collection of objects</p></blockquote>
<p>This is actually a major gotcha &#8212; NodeLists are late bound, which can cause quite some confusion, if you iterate over it and manipulate the DOM at the same time. For this reason alone, it&#8217;s usually a good idea to convert it into an array.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Tino Zijdel</title>
		<link>http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-658004</link>
		<dc:creator>Tino Zijdel</dc:creator>
		<pubDate>Thu, 20 Mar 2008 14:53:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-658004</guid>
		<description>Here's a nice way to convert a nodeList to an array (it uses the Array.slice() method on the nodeList itself): &lt;code&gt;var nodeArray = [].slice.call(nodeList, 0);&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Here&#8217;s a nice way to convert a nodeList to an array (it uses the Array.slice() method on the nodeList itself): <code>var nodeArray = [].slice.call(nodeList, 0);</code></p>]]></content:encoded>
	</item>
	<item>
		<title>By: cranial-bore</title>
		<link>http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-657965</link>
		<dc:creator>cranial-bore</dc:creator>
		<pubDate>Thu, 20 Mar 2008 13:21:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-657965</guid>
		<description>Thanks for that. I knew a collection wasn't an array, but I thought that for all intents and purposes they were the same. Good to know what the practical difference really is.

Does this mean that you couldn't add an event listener to an array element (as per this collection-&#62;array function) ?</description>
		<content:encoded><![CDATA[<p>Thanks for that. I knew a collection wasn&#8217;t an array, but I thought that for all intents and purposes they were the same. Good to know what the practical difference really is.</p>
<p>Does this mean that you couldn&#8217;t add an event listener to an array element (as per this collection-&gt;array function) ?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: AutisticCuckoo</title>
		<link>http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-657427</link>
		<dc:creator>AutisticCuckoo</dc:creator>
		<pubDate>Wed, 19 Mar 2008 07:47:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2008/03/19/a-collection-is-not-an-array/#comment-657427</guid>
		<description>Good summary, James! That last point is quite important if you're dynamically modifying the collection inside a loop.</description>
		<content:encoded><![CDATA[<p>Good summary, James! That last point is quite important if you&#8217;re dynamically modifying the collection inside a loop.</p>]]></content:encoded>
	</item>
</channel>
</rss>
