<?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: for in Intrigue</title>
	<atom:link href="http://www.yuiblog.com/blog/index.php/2006/09/26/for-in-intrigue/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.yuiblog.com/blog/2006/09/26/for-in-intrigue/</link>
	<description>The official blog of the YUI Project.</description>
	<lastBuildDate>Thu, 09 Feb 2012 01:46:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
	<item>
		<title>By: Thom</title>
		<link>http://www.yuiblog.com/blog/2006/09/26/for-in-intrigue/comment-page-1/#comment-588456</link>
		<dc:creator>Thom</dc:creator>
		<pubDate>Mon, 22 Feb 2010 08:39:02 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2006/09/26/for-in-intrigue/#comment-588456</guid>
		<description>@Paul

That&#039;s the style I&#039;d use, ever since I learned it from Wil Shipley&#039;s blog (Delicious Monster) for reducing nesting and improving readability, as you said. Essentially, set up conditionals to bail out immediately if the there&#039;s nothing to do because of the circumstances not being met. Great suggestion, IMHO.</description>
		<content:encoded><![CDATA[<p>@Paul</p>
<p>That&#8217;s the style I&#8217;d use, ever since I learned it from Wil Shipley&#8217;s blog (Delicious Monster) for reducing nesting and improving readability, as you said. Essentially, set up conditionals to bail out immediately if the there&#8217;s nothing to do because of the circumstances not being met. Great suggestion, IMHO.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Dunbar</title>
		<link>http://www.yuiblog.com/blog/2006/09/26/for-in-intrigue/comment-page-1/#comment-585339</link>
		<dc:creator>Paul Dunbar</dc:creator>
		<pubDate>Wed, 14 Oct 2009 19:23:45 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2006/09/26/for-in-intrigue/#comment-585339</guid>
		<description>When processing for (p in o) loops, is there anything wrong with the continue statement?  I prefer to do the following:
&lt;code&gt;
for (p in o) {
   if (!o.hasOwnProperty(p)) {
      continue;
   };
   // more code
}
&lt;/code&gt;
I find that this helps to reduce nesting and makes for more readable code.</description>
		<content:encoded><![CDATA[<p>When processing for (p in o) loops, is there anything wrong with the continue statement?  I prefer to do the following:<br />
<code><br />
for (p in o) {<br />
   if (!o.hasOwnProperty(p)) {<br />
      continue;<br />
   };<br />
   // more code<br />
}<br />
</code><br />
I find that this helps to reduce nesting and makes for more readable code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen</title>
		<link>http://www.yuiblog.com/blog/2006/09/26/for-in-intrigue/comment-page-1/#comment-489486</link>
		<dc:creator>Stephen</dc:creator>
		<pubDate>Wed, 08 Oct 2008 01:44:17 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2006/09/26/for-in-intrigue/#comment-489486</guid>
		<description>for those who are interested, i&#039;m using the following to allow for more php-style trim() (where specific characters/strings can be trimmed from the string):
&lt;code&gt;

if (!String.prototype.trim) {
	String.prototype.trim = function (chars) {
		var that = this.toString();
		chars = chars &#124;&#124; &#039;\\s&#039;;
		if (Array.isArray(chars)) {
			chars = chars.join(&#039;&#124;&#039;);
		}
		var re = new RegExp(&#039;^(&#039; + chars + &#039;)+&#124;(&#039; + chars + &#039;)+$&#039;,&#039;g&#039;);
		return that.replace(re,&#039;&#039;);
	}
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>for those who are interested, i&#8217;m using the following to allow for more php-style trim() (where specific characters/strings can be trimmed from the string):<br />
<code></p>
<p>if (!String.prototype.trim) {<br />
	String.prototype.trim = function (chars) {<br />
		var that = this.toString();<br />
		chars = chars || '\\s';<br />
		if (Array.isArray(chars)) {<br />
			chars = chars.join('|');<br />
		}<br />
		var re = new RegExp('^(' + chars + ')+|(' + chars + ')+$','g');<br />
		return that.replace(re,'');<br />
	}<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Emmanuel Oga</title>
		<link>http://www.yuiblog.com/blog/2006/09/26/for-in-intrigue/comment-page-1/#comment-488264</link>
		<dc:creator>Emmanuel Oga</dc:creator>
		<pubDate>Mon, 06 Oct 2008 13:00:03 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2006/09/26/for-in-intrigue/#comment-488264</guid>
		<description>Is this too ugly?

&lt;code&gt;
for_own(objects, function (o) {
  // ...
});
&lt;/code&gt;

Where for_own would be something like:

&lt;code&gt;
function for_own(obj, fun) {
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            fun(key);
        }
    }
};
&lt;/code&gt;

Greets!</description>
		<content:encoded><![CDATA[<p>Is this too ugly?</p>
<p><code><br />
for_own(objects, function (o) {<br />
  // ...<br />
});<br />
</code></p>
<p>Where for_own would be something like:</p>
<p><code><br />
function for_own(obj, fun) {<br />
    for (var key in obj) {<br />
        if (obj.hasOwnProperty(key)) {<br />
            fun(key);<br />
        }<br />
    }<br />
};<br />
</code></p>
<p>Greets!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stefan</title>
		<link>http://www.yuiblog.com/blog/2006/09/26/for-in-intrigue/comment-page-1/#comment-412392</link>
		<dc:creator>Stefan</dc:creator>
		<pubDate>Fri, 04 Jul 2008 21:31:11 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2006/09/26/for-in-intrigue/#comment-412392</guid>
		<description>Looks to me like additional shallow versions of the &lt;i&gt;in&lt;/i&gt; operator and the &lt;i&gt;for in&lt;/i&gt; loop statement (which do not follow the prototype chain) would be a useful extension of the language.</description>
		<content:encoded><![CDATA[<p>Looks to me like additional shallow versions of the <i>in</i> operator and the <i>for in</i> loop statement (which do not follow the prototype chain) would be a useful extension of the language.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: byron</title>
		<link>http://www.yuiblog.com/blog/2006/09/26/for-in-intrigue/comment-page-1/#comment-394670</link>
		<dc:creator>byron</dc:creator>
		<pubDate>Wed, 11 Jun 2008 22:53:09 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2006/09/26/for-in-intrigue/#comment-394670</guid>
		<description>First an editorial, then some science.

It&#039;s just wrong to say that a js Object is not a prop/value hash, should not be used as such, and then go on to implement an alternate hash mechanism. Objects certainly are hashes (dictionaries, key/value pairs), but they don&#039;t behave exactly like hashes in some other languages.  Yes, because of prototyping and inheritance a js hash can get additional members.  Using hasOwnProperty is a correct way to filter these - that is, it&#039;s a correct way to use an object hash in javascript.  Do you really want to write a custom hash object just to avoid the one-line property check?  Like so many libraries and toolkits out there, this smacks as an effort to turn javascript into a different language.  These efforts usually result in overhead, obfuscation and maintenance challenges.

Ok, the science.  I wanted to know the overhead of adding a hasOwnProperty() check.  I also timed the alternate (inferior) &quot;typeof obj[key] !== &#039;function&#039;&quot; check.  Instead of over-burdening this comment with the full results, if you&#039;re interested, the engine is at &lt;a href=&quot;http://randomous.com/tools/misc/timer.php&quot; title=&quot;Javascript Timer&quot; rel=&quot;nofollow&quot;&gt;http://randomous.com/tools/misc/timer.php&lt;/a&gt;. The results of timing tests relevant to this article are at the bottom of that page.

Cheers...</description>
		<content:encoded><![CDATA[<p>First an editorial, then some science.</p>
<p>It&#8217;s just wrong to say that a js Object is not a prop/value hash, should not be used as such, and then go on to implement an alternate hash mechanism. Objects certainly are hashes (dictionaries, key/value pairs), but they don&#8217;t behave exactly like hashes in some other languages.  Yes, because of prototyping and inheritance a js hash can get additional members.  Using hasOwnProperty is a correct way to filter these &#8211; that is, it&#8217;s a correct way to use an object hash in javascript.  Do you really want to write a custom hash object just to avoid the one-line property check?  Like so many libraries and toolkits out there, this smacks as an effort to turn javascript into a different language.  These efforts usually result in overhead, obfuscation and maintenance challenges.</p>
<p>Ok, the science.  I wanted to know the overhead of adding a hasOwnProperty() check.  I also timed the alternate (inferior) &#8220;typeof obj[key] !== &#8216;function&#8217;&#8221; check.  Instead of over-burdening this comment with the full results, if you&#8217;re interested, the engine is at <a href="http://randomous.com/tools/misc/timer.php" title="Javascript Timer" rel="nofollow">http://randomous.com/tools/misc/timer.php</a>. The results of timing tests relevant to this article are at the bottom of that page.</p>
<p>Cheers&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: henrah</title>
		<link>http://www.yuiblog.com/blog/2006/09/26/for-in-intrigue/comment-page-1/#comment-339433</link>
		<dc:creator>henrah</dc:creator>
		<pubDate>Fri, 04 Apr 2008 12:27:01 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2006/09/26/for-in-intrigue/#comment-339433</guid>
		<description>@Douglas: I know this article is 5 months old, but since it is being linked by JSLint, you should really fix that last code sample so that it doesn&#039;t return true for undefined keys.
&lt;code&gt;
function check_word(word) {
    return typeof words[word] !== &#039;function&#039;
        &amp;&amp; typeof words[word] !== &#039;undefined&#039;
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>@Douglas: I know this article is 5 months old, but since it is being linked by JSLint, you should really fix that last code sample so that it doesn&#8217;t return true for undefined keys.<br />
<code><br />
function check_word(word) {<br />
    return typeof words[word] !== 'function'<br />
        &amp;&amp; typeof words[word] !== 'undefined'<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: GUYA.NET &#187; Blog Archive &#187; Resolving some issues with swfobject</title>
		<link>http://www.yuiblog.com/blog/2006/09/26/for-in-intrigue/comment-page-1/#comment-224553</link>
		<dc:creator>GUYA.NET &#187; Blog Archive &#187; Resolving some issues with swfobject</dc:creator>
		<pubDate>Sun, 11 Nov 2007 18:17:17 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2006/09/26/for-in-intrigue/#comment-224553</guid>
		<description>[...] More related info about hasOwnProperty. [...]</description>
		<content:encoded><![CDATA[<p>[...] More related info about hasOwnProperty. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Garrett</title>
		<link>http://www.yuiblog.com/blog/2006/09/26/for-in-intrigue/comment-page-1/#comment-189184</link>
		<dc:creator>Garrett</dc:creator>
		<pubDate>Tue, 11 Sep 2007 21:21:28 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2006/09/26/for-in-intrigue/#comment-189184</guid>
		<description>correction: 
For programmer-defined objects, &lt;code&gt;propertyIsEnumerable&lt;/code&gt; is specified such that coforming implementations will have the exact same result as with &lt;code&gt;hasOwnProperty&lt;/code&gt;.

&lt;code&gt;var obj = {
   Garrett : &quot;monkeybrains&quot;
   ,borked : &quot;javascript&quot; // transitivity fixed :-)
   ,toString : function() { return &quot;an enumerable prop&quot;; }
   ,valueOf : function() { return 2; } // also enumerable.
};&lt;/code&gt;

// can this be false for any value of s?
(obj.hasOwnProperty( s ) != obj.propertyIsEnumerable( s ))</description>
		<content:encoded><![CDATA[<p>correction:<br />
For programmer-defined objects, <code>propertyIsEnumerable</code> is specified such that coforming implementations will have the exact same result as with <code>hasOwnProperty</code>.</p>
<p><code>var obj = {<br />
   Garrett : "monkeybrains"<br />
   ,borked : "javascript" // transitivity fixed :-)<br />
   ,toString : function() { return "an enumerable prop"; }<br />
   ,valueOf : function() { return 2; } // also enumerable.<br />
};</code></p>
<p>// can this be false for any value of s?<br />
(obj.hasOwnProperty( s ) != obj.propertyIsEnumerable( s ))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Garrett</title>
		<link>http://www.yuiblog.com/blog/2006/09/26/for-in-intrigue/comment-page-1/#comment-188494</link>
		<dc:creator>Garrett</dc:creator>
		<pubDate>Mon, 10 Sep 2007 07:42:01 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2006/09/26/for-in-intrigue/#comment-188494</guid>
		<description>Checking the object by using typeof can in no way be considered a replacement for &lt;code&gt;hasOwnProperty&lt;/code&gt;. It is quite likely that a functional value exists in the iterated object&#039;s instance (a method).

Using &quot;in&quot; will check the prototype chain. 

This feature can be useful for examining the prototype chain. For example, if you wanted to generically advise each method in a Panel 
class, you could iterate through all the functions in the object&#039;s prototype chain.
If you think about it, it can be a useful feature.

In fact, the &quot;flaw&quot; that Doug mentions is intentional behavior of the language. As mentioned in earlier comments, it is a perfectly valid feature (read above).

Sweeney mentioned using propertyIsEnumerable to check an object&#039;s properties in lieu of missing hasOwnProperty. 

Interestingly, &lt;code&gt;propertyIsEnumerable&lt;/code&gt; is specified such that coforming implementations will have the exact same result as with &lt;code&gt;hasOwnProperty&lt;/code&gt;.

Method &lt;code&gt;propertyIsEnumerable&lt;/code&gt; does not check the prototype chain (15.2.4.7). Since all programmer-defined properties are enumerable. Therefore, &lt;code&gt;propertyIsEnumerable&lt;/code&gt; provides the behavior that Doug so desires. 

The fact that &lt;code&gt;propertyIsEnumerable&lt;/code&gt; does not check the prototype chain is itself a bug of the spec. David Flanagan pointed this out and Brendan will readily admit that it&#039;s a bug. JScript and SpiderMonkey both implement the spec correctly in this aspect.

The fact that &lt;code&gt;propertyIsEnumerable&lt;/code&gt; does not check the prototype chain is, in fact, a bug that

Unfortunately, JScript suffers from another bug that causes &lt;code&gt;propertyIsEnumerable&lt;/code&gt; to return false on a user-defined object property.

This happens when the value that is passed to &lt;code&gt;propertyIsEnumerable&lt;/code&gt; exists in the instance and overrides a non-enumerable (&lt;code&gt;DontEnum&lt;/code&gt;) property in the object&#039;s prototype chain (such as an overridden &quot;toString&quot;)

Matthew Frank provides a tighter version of trim, and there&#039;s nothing wrong with adding &lt;code&gt;trim&lt;/code&gt;, however, it should be done conditionally:
&lt;code&gt;if(!String.prototype.trim)
    String.prototype.trim = function () {
        return this.replace(/^\s+&#124;\s+$/g, &#039;&#039;);
};&lt;/code&gt;

The conditional check is necessary because future versions of ECMAScript will have &lt;code&gt;String.prototype.trim&lt;/code&gt;.

@Andrew Dupont the &lt;code&gt;!==&lt;/code&gt; will produce the same result as &lt;code&gt;!=&lt;/code&gt; when both operands are string values (returns false if the sequence of characters is not an exact match,). I would probably opt for &lt;code&gt;!=&lt;/code&gt; though.</description>
		<content:encoded><![CDATA[<p>Checking the object by using typeof can in no way be considered a replacement for <code>hasOwnProperty</code>. It is quite likely that a functional value exists in the iterated object&#8217;s instance (a method).</p>
<p>Using &#8220;in&#8221; will check the prototype chain. </p>
<p>This feature can be useful for examining the prototype chain. For example, if you wanted to generically advise each method in a Panel<br />
class, you could iterate through all the functions in the object&#8217;s prototype chain.<br />
If you think about it, it can be a useful feature.</p>
<p>In fact, the &#8220;flaw&#8221; that Doug mentions is intentional behavior of the language. As mentioned in earlier comments, it is a perfectly valid feature (read above).</p>
<p>Sweeney mentioned using propertyIsEnumerable to check an object&#8217;s properties in lieu of missing hasOwnProperty. </p>
<p>Interestingly, <code>propertyIsEnumerable</code> is specified such that coforming implementations will have the exact same result as with <code>hasOwnProperty</code>.</p>
<p>Method <code>propertyIsEnumerable</code> does not check the prototype chain (15.2.4.7). Since all programmer-defined properties are enumerable. Therefore, <code>propertyIsEnumerable</code> provides the behavior that Doug so desires. </p>
<p>The fact that <code>propertyIsEnumerable</code> does not check the prototype chain is itself a bug of the spec. David Flanagan pointed this out and Brendan will readily admit that it&#8217;s a bug. JScript and SpiderMonkey both implement the spec correctly in this aspect.</p>
<p>The fact that <code>propertyIsEnumerable</code> does not check the prototype chain is, in fact, a bug that</p>
<p>Unfortunately, JScript suffers from another bug that causes <code>propertyIsEnumerable</code> to return false on a user-defined object property.</p>
<p>This happens when the value that is passed to <code>propertyIsEnumerable</code> exists in the instance and overrides a non-enumerable (<code>DontEnum</code>) property in the object&#8217;s prototype chain (such as an overridden &#8220;toString&#8221;)</p>
<p>Matthew Frank provides a tighter version of trim, and there&#8217;s nothing wrong with adding <code>trim</code>, however, it should be done conditionally:<br />
<code>if(!String.prototype.trim)<br />
    String.prototype.trim = function () {<br />
        return this.replace(/^\s+|\s+$/g, '');<br />
};</code></p>
<p>The conditional check is necessary because future versions of ECMAScript will have <code>String.prototype.trim</code>.</p>
<p>@Andrew Dupont the <code>!==</code> will produce the same result as <code>!=</code> when both operands are string values (returns false if the sequence of characters is not an exact match,). I would probably opt for <code>!=</code> though.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

