<?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: Using For In Loops Safely</title>
	<atom:link href="http://mattsnider.com/languages/javascript/using-for-in-loops-safely/feed/" rel="self" type="application/rss+xml" />
	<link>http://mattsnider.com/languages/javascript/using-for-in-loops-safely/</link>
	<description>Understanding JavaScript and Frameworks</description>
	<pubDate>Wed, 07 Jan 2009 14:04:36 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Matt Snider</title>
		<link>http://mattsnider.com/languages/javascript/using-for-in-loops-safely/comment-page-1/#comment-7925</link>
		<dc:creator>Matt Snider</dc:creator>
		<pubDate>Fri, 21 Mar 2008 15:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://mattsnider.com/javascript/using-for-in-loops-safely/#comment-7925</guid>
		<description>Vijaykant, you can link to anything you want. I only ask that if you copy anything from this blog, you also reference it.</description>
		<content:encoded><![CDATA[<p>Vijaykant, you can link to anything you want. I only ask that if you copy anything from this blog, you also reference it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vijaykant</title>
		<link>http://mattsnider.com/languages/javascript/using-for-in-loops-safely/comment-page-1/#comment-7915</link>
		<dc:creator>Vijaykant</dc:creator>
		<pubDate>Fri, 21 Mar 2008 11:13:42 +0000</pubDate>
		<guid isPermaLink="false">http://mattsnider.com/javascript/using-for-in-loops-safely/#comment-7915</guid>
		<description>I liked the content of the blog. can i use the link as an educational reference?</description>
		<content:encoded><![CDATA[<p>I liked the content of the blog. can i use the link as an educational reference?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: the DtTvB</title>
		<link>http://mattsnider.com/languages/javascript/using-for-in-loops-safely/comment-page-1/#comment-7270</link>
		<dc:creator>the DtTvB</dc:creator>
		<pubDate>Fri, 14 Mar 2008 04:49:09 +0000</pubDate>
		<guid isPermaLink="false">http://mattsnider.com/javascript/using-for-in-loops-safely/#comment-7270</guid>
		<description>The first time I play with XMLHttpRequest, I use for..in loops with childNodes and getElementsByTagName, like this:

&lt;code&gt;for (var i in items) { ... }&lt;/code&gt;

Then it always gave me an error, and I don't understand why.

I later know that i was "length".

Very good technique. Too bad I don't do so much OOPs.</description>
		<content:encoded><![CDATA[<p>The first time I play with XMLHttpRequest, I use for..in loops with childNodes and getElementsByTagName, like this:</p>
<p><code>for (var i in items) { ... }</code></p>
<p>Then it always gave me an error, and I don&#8217;t understand why.</p>
<p>I later know that i was &#8220;length&#8221;.</p>
<p>Very good technique. Too bad I don&#8217;t do so much OOPs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dimitry</title>
		<link>http://mattsnider.com/languages/javascript/using-for-in-loops-safely/comment-page-1/#comment-6563</link>
		<dc:creator>Dimitry</dc:creator>
		<pubDate>Tue, 04 Mar 2008 19:33:58 +0000</pubDate>
		<guid isPermaLink="false">http://mattsnider.com/javascript/using-for-in-loops-safely/#comment-6563</guid>
		<description>Recently we've gotten away from extending the Object at all.

The new YUI JSON singleton provides the same functionality without adding Object.toJsonString (Instead: YAHOO.lang.JSON.parse() or .stringify())

Although, safety with for-in loops should always be considered (in JS anyways).</description>
		<content:encoded><![CDATA[<p>Recently we&#8217;ve gotten away from extending the Object at all.</p>
<p>The new YUI JSON singleton provides the same functionality without adding Object.toJsonString (Instead: YAHOO.lang.JSON.parse() or .stringify())</p>
<p>Although, safety with for-in loops should always be considered (in JS anyways).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Snider</title>
		<link>http://mattsnider.com/languages/javascript/using-for-in-loops-safely/comment-page-1/#comment-6552</link>
		<dc:creator>Matt Snider</dc:creator>
		<pubDate>Tue, 04 Mar 2008 16:08:27 +0000</pubDate>
		<guid isPermaLink="false">http://mattsnider.com/javascript/using-for-in-loops-safely/#comment-6552</guid>
		<description>Thanks for your comments everyone.

David, although, I agree that Associative JavaScript Arrays technically don't exist (they are simply objects) and should never be used. It is still possible to syntactically create them.

Florin, you are absolutely right, it is much faster to know what you are testing for and just test against it. I however, have used Functions as members of an Array on more than one occasion.

Mills, I like your approach and will have to explore it more.</description>
		<content:encoded><![CDATA[<p>Thanks for your comments everyone.</p>
<p>David, although, I agree that Associative JavaScript Arrays technically don&#8217;t exist (they are simply objects) and should never be used. It is still possible to syntactically create them.</p>
<p>Florin, you are absolutely right, it is much faster to know what you are testing for and just test against it. I however, have used Functions as members of an Array on more than one occasion.</p>
<p>Mills, I like your approach and will have to explore it more.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MillsJROSS</title>
		<link>http://mattsnider.com/languages/javascript/using-for-in-loops-safely/comment-page-1/#comment-6461</link>
		<dc:creator>MillsJROSS</dc:creator>
		<pubDate>Mon, 03 Mar 2008 14:02:07 +0000</pubDate>
		<guid isPermaLink="false">http://mattsnider.com/javascript/using-for-in-loops-safely/#comment-6461</guid>
		<description>When I am looking for a JavaScript library, I'm almost always instantly turned off if I find they've done something to extend native objects. It's a red flag for me that they don't necessarily care how their code interacts with another library or your hand coded functions. I understand the urge to fill in the gaps you might feel JavaScript has left out, but there's other ways around that that work well with other code.

As an example. I want any object I use in my library to be able to find their own attributes.
So here's how I might go about doing that...

var ExtendedObject = function(obj) {
    for(var attr in obj) {this[attr] = obj[attr];
}

ExtendedObject.prototype.findAttribute = () {
   ...//Put code here
}

This allows me to create my own extended object like this.

var obj = new ExtendedObject({test: 'test'});

The native object has not been changed, so someone else is free to use the object however they wish. You can determine if the object is of type ExtendedObject by using the instanceof operator. Additionaly, you'd probably want to have an ExtendedObject.prototype.stripObject function that filters your extended object. 

The only major drawback is that you have to use  "new ExtendedObject" (or whatever you choose to call it), everytime you want to create an object, rather than just "{}". It's only a drawback in size, though...in return you get code that works well with others.</description>
		<content:encoded><![CDATA[<p>When I am looking for a JavaScript library, I&#8217;m almost always instantly turned off if I find they&#8217;ve done something to extend native objects. It&#8217;s a red flag for me that they don&#8217;t necessarily care how their code interacts with another library or your hand coded functions. I understand the urge to fill in the gaps you might feel JavaScript has left out, but there&#8217;s other ways around that that work well with other code.</p>
<p>As an example. I want any object I use in my library to be able to find their own attributes.<br />
So here&#8217;s how I might go about doing that&#8230;</p>
<p>var ExtendedObject = function(obj) {<br />
    for(var attr in obj) {this[attr] = obj[attr];<br />
}</p>
<p>ExtendedObject.prototype.findAttribute = () {<br />
   &#8230;//Put code here<br />
}</p>
<p>This allows me to create my own extended object like this.</p>
<p>var obj = new ExtendedObject({test: &#8216;test&#8217;});</p>
<p>The native object has not been changed, so someone else is free to use the object however they wish. You can determine if the object is of type ExtendedObject by using the instanceof operator. Additionaly, you&#8217;d probably want to have an ExtendedObject.prototype.stripObject function that filters your extended object. </p>
<p>The only major drawback is that you have to use  &#8220;new ExtendedObject&#8221; (or whatever you choose to call it), everytime you want to create an object, rather than just &#8220;{}&#8221;. It&#8217;s only a drawback in size, though&#8230;in return you get code that works well with others.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Florin</title>
		<link>http://mattsnider.com/languages/javascript/using-for-in-loops-safely/comment-page-1/#comment-6315</link>
		<dc:creator>Florin</dc:creator>
		<pubDate>Sat, 01 Mar 2008 22:04:43 +0000</pubDate>
		<guid isPermaLink="false">http://mattsnider.com/javascript/using-for-in-loops-safely/#comment-6315</guid>
		<description>Asociative arrays = objects :)

anyways i find it easyer and faster to test the type of the element in the loop depending on the situation.
for (var i in obj) {
  if (typeof obj[i] != 'function') {
  }
}

I guess your code is very usefull when u nead to iterate thru methods also.</description>
		<content:encoded><![CDATA[<p>Asociative arrays = objects <img src='http://mattsnider.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>anyways i find it easyer and faster to test the type of the element in the loop depending on the situation.<br />
for (var i in obj) {<br />
  if (typeof obj[i] != &#8216;function&#8217;) {<br />
  }<br />
}</p>
<p>I guess your code is very usefull when u nead to iterate thru methods also.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Golightly</title>
		<link>http://mattsnider.com/languages/javascript/using-for-in-loops-safely/comment-page-1/#comment-6312</link>
		<dc:creator>David Golightly</dc:creator>
		<pubDate>Sat, 01 Mar 2008 20:42:53 +0000</pubDate>
		<guid isPermaLink="false">http://mattsnider.com/javascript/using-for-in-loops-safely/#comment-6312</guid>
		<description>We should finally let the erroneous notion of "Associative Array" in JavaScript die.  There is no such thing, and it's irresponsible to keep using the term.  Arrays should never, ever be used to store name-value pairs - they should only be used to store items indexed to an integer.  Object literals serve the purpose that Associative Arrays serve in other languages.

With this discipline, it's perfectly acceptable to extend Array.prototype since you shouldn't be iterating over arrays using for .. in anyway.  Use a conventional for or while loop instead, and leave for .. in to object literals.  (Of course, it's still bad practice to extend Object.prototype for this reason.)</description>
		<content:encoded><![CDATA[<p>We should finally let the erroneous notion of &#8220;Associative Array&#8221; in JavaScript die.  There is no such thing, and it&#8217;s irresponsible to keep using the term.  Arrays should never, ever be used to store name-value pairs - they should only be used to store items indexed to an integer.  Object literals serve the purpose that Associative Arrays serve in other languages.</p>
<p>With this discipline, it&#8217;s perfectly acceptable to extend Array.prototype since you shouldn&#8217;t be iterating over arrays using for .. in anyway.  Use a conventional for or while loop instead, and leave for .. in to object literals.  (Of course, it&#8217;s still bad practice to extend Object.prototype for this reason.)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
