<?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: Function.prototype.apply&#160;revisited</title>
	<atom:link href="http://hexmen.com/blog/2006/12/revisiting-functionprototypeapply-for-ie5/feed/" rel="self" type="application/rss+xml" />
	<link>http://hexmen.com/blog/2006/12/revisiting-functionprototypeapply-for-ie5/</link>
	<description>On programming, and other things...</description>
	<lastBuildDate>Mon, 15 Mar 2010 23:47:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Ash</title>
		<link>http://hexmen.com/blog/2006/12/revisiting-functionprototypeapply-for-ie5/comment-page-1/#comment-55</link>
		<dc:creator>Ash</dc:creator>
		<pubDate>Thu, 15 Feb 2007 09:35:52 +0000</pubDate>
		<guid isPermaLink="false">http://hexmen.com/blog/2006/05/revisiting-functionprototypeapply-for-ie5/#comment-55</guid>
		<description>That looks like a really good function, and (at the moment) the only way I can break it is through sabotage...

To demonstrate:
&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&#039;tan&#039; in Math;                            // true
Math.hasOwnProperty(&#039;tan&#039;);               // true
Object.hasProperty(Math, &#039;tan&#039;);          // true

Math.tan = void(0)

&#039;tan&#039; in Math;                            // true
Math.hasOwnProperty(&#039;tan&#039;);               // true
Object.hasProperty(Math, &#039;tan&#039;);          // false
&lt;/code&gt;&lt;/pre&gt;

Although it can&#039;t handle contrived sabotage; I think the revised &lt;code&gt;hasProperty&lt;/code&gt; is &#039;good enough&#039; and I can&#039;t see how it could be improved.</description>
		<content:encoded><![CDATA[<p>That looks like a really good function, and (at the moment) the only way I can break it is through sabotage&#8230;</p>
<p>To demonstrate:</p>
<pre><code class="javascript">'tan' in Math;                            // true
Math.hasOwnProperty('tan');               // true
Object.hasProperty(Math, 'tan');          // true

Math.tan = void(0)

'tan' in Math;                            // true
Math.hasOwnProperty('tan');               // true
Object.hasProperty(Math, 'tan');          // false
</code></pre>
<p>Although it can&#8217;t handle contrived sabotage; I think the revised <code>hasProperty</code> is &#8216;good enough&#8217; and I can&#8217;t see how it could be improved.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Márcio Faustino</title>
		<link>http://hexmen.com/blog/2006/12/revisiting-functionprototypeapply-for-ie5/comment-page-1/#comment-53</link>
		<dc:creator>Márcio Faustino</dc:creator>
		<pubDate>Wed, 14 Feb 2007 21:37:08 +0000</pubDate>
		<guid isPermaLink="false">http://hexmen.com/blog/2006/05/revisiting-functionprototypeapply-for-ie5/#comment-53</guid>
		<description>Hmmm... looks like I was over-confident :-)
How about this one:

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Object.hasProperty = function(object, property) {
	if (typeof(object[property]) != &#039;undefined&#039;) {
		return true;
	}
	
	for (var p in object) {
		if (p == property) {
			return true;
		}
	}
	
	return false;
};

Object.hasProperty({}, &#039;toString&#039;);       // true
Object.hasProperty([], &#039;length&#039;);         // true

({}).hasOwnProperty(&#039;toString&#039;);          // false
[].hasOwnProperty(&#039;length&#039;);              // true

Object.hasProperty({a: void(0)}, &#039;a&#039;);    // true
Object.hasProperty({}, &#039;undefined&#039;);      // false&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hmmm&#8230; looks like I was over-confident <img src='http://hexmen.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
How about this one:</p>
<pre><code class="javascript">Object.hasProperty = function(object, property) {
	if (typeof(object[property]) != 'undefined') {
		return true;
	}

	for (var p in object) {
		if (p == property) {
			return true;
		}
	}

	return false;
};

Object.hasProperty({}, 'toString');       // true
Object.hasProperty([], 'length');         // true

({}).hasOwnProperty('toString');          // false
[].hasOwnProperty('length');              // true

Object.hasProperty({a: void(0)}, 'a');    // true
Object.hasProperty({}, 'undefined');      // false</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ash</title>
		<link>http://hexmen.com/blog/2006/12/revisiting-functionprototypeapply-for-ie5/comment-page-1/#comment-48</link>
		<dc:creator>Ash</dc:creator>
		<pubDate>Sun, 11 Feb 2007 19:11:12 +0000</pubDate>
		<guid isPermaLink="false">http://hexmen.com/blog/2006/05/revisiting-functionprototypeapply-for-ie5/#comment-48</guid>
		<description>Interesting... I shouldn&#039;t have suggested using &lt;code&gt;hasOwnProperty&lt;/code&gt; to check for an object property, as it doesn&#039;t consider the prototype chain.

Márcio, your &lt;code&gt;Object.hasProperty&lt;/code&gt; function is closer in function to &lt;code&gt;hasOwnProperty&lt;/code&gt; than to &lt;code&gt;in&lt;/code&gt;.

The difference is clear when checking for a &#039;length&#039; property on an array, or checking for &#039;toString&#039; on any object:
&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&#039;toString&#039; in a;   // true
Object.hasProperty(a, &#039;toString&#039;);  // false

&#039;length&#039; in []; // true
Object.hasProperty([], &#039;length&#039;); // false 
// but... this isn&#039;t the same as hasOwnProperty either:
[].hasOwnProperty(&#039;length&#039;); // true&lt;/code&gt;&lt;/pre&gt;

As I said before, I don&#039;t think there&#039;s a perfect fix; but I&#039;m sure there are cases where your &lt;code&gt;hasProperty&lt;/code&gt; function could come in handy.

(The differences between &lt;code&gt;in&lt;/code&gt; used as an operator, and &lt;code&gt;in&lt;/code&gt; used in a &lt;code&gt;for-in&lt;/code&gt; statement are defined in sections 11.8.7 and 12.6.4 of the &lt;a href=&quot;http://www.mozilla.org/js/language/E262-3.pdf&quot; rel=&quot;nofollow&quot;&gt;ECMAScript Language Spec&lt;/a&gt;.)</description>
		<content:encoded><![CDATA[<p>Interesting&#8230; I shouldn&#8217;t have suggested using <code>hasOwnProperty</code> to check for an object property, as it doesn&#8217;t consider the prototype chain.</p>
<p>Márcio, your <code>Object.hasProperty</code> function is closer in function to <code>hasOwnProperty</code> than to <code>in</code>.</p>
<p>The difference is clear when checking for a &#8216;length&#8217; property on an array, or checking for &#8216;toString&#8217; on any object:</p>
<pre><code class="javascript">'toString' in a;   // true
Object.hasProperty(a, 'toString');  // false

'length' in []; // true
Object.hasProperty([], 'length'); // false
// but... this isn't the same as hasOwnProperty either:
[].hasOwnProperty('length'); // true</code></pre>
<p>As I said before, I don&#8217;t think there&#8217;s a perfect fix; but I&#8217;m sure there are cases where your <code>hasProperty</code> function could come in handy.</p>
<p>(The differences between <code>in</code> used as an operator, and <code>in</code> used in a <code>for-in</code> statement are defined in sections 11.8.7 and 12.6.4 of the <a href="http://www.mozilla.org/js/language/E262-3.pdf" rel="nofollow">ECMAScript Language Spec</a>.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Márcio Faustino</title>
		<link>http://hexmen.com/blog/2006/12/revisiting-functionprototypeapply-for-ie5/comment-page-1/#comment-47</link>
		<dc:creator>Márcio Faustino</dc:creator>
		<pubDate>Sun, 11 Feb 2007 16:18:56 +0000</pubDate>
		<guid isPermaLink="false">http://hexmen.com/blog/2006/05/revisiting-functionprototypeapply-for-ie5/#comment-47</guid>
		<description>You&#039;re right. But for IE4 and up, you can use this:
&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Object.hasProperty = function(object, property) {
	for (var p in object) {
		if (p == property) {
			return true;
		}
	}
	
	return false;
};&lt;/code&gt;&lt;/pre&gt;
 
For example:
 
&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;var a = {b: void(0)};
 
typeof(a.c);    // &#039;undefined&#039;
typeof(a.b);    // &#039;undefined&#039;
 
&#039;c&#039; in a;       // false
&#039;b&#039; in a;       // true
 
// Cross-browser:
Object.hasProperty(a, &#039;c&#039;);    // false
Object.hasProperty(a, &#039;b&#039;);    // true
delete a.b;
Object.hasProperty(a, &#039;b&#039;);    // false&lt;/code&gt;&lt;/pre&gt;
 
Also, don&#039;t forget you can use the syntax:  &lt;code&gt;in&lt;/code&gt; , instead of &lt;code&gt;hasOwnProperty&lt;/code&gt;.</description>
		<content:encoded><![CDATA[<p>You&#8217;re right. But for IE4 and up, you can use this:</p>
<pre><code class="javascript">Object.hasProperty = function(object, property) {
	for (var p in object) {
		if (p == property) {
			return true;
		}
	}

	return false;
};</code></pre>
<p>For example:</p>
<pre><code class="javascript">var a = {b: void(0)};

typeof(a.c);    // 'undefined'
typeof(a.b);    // 'undefined'

'c' in a;       // false
'b' in a;       // true

// Cross-browser:
Object.hasProperty(a, 'c');    // false
Object.hasProperty(a, 'b');    // true
delete a.b;
Object.hasProperty(a, 'b');    // false</code></pre>
<p>Also, don&#8217;t forget you can use the syntax:  <code>in</code> , instead of <code>hasOwnProperty</code>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ash</title>
		<link>http://hexmen.com/blog/2006/12/revisiting-functionprototypeapply-for-ie5/comment-page-1/#comment-46</link>
		<dc:creator>Ash</dc:creator>
		<pubDate>Fri, 09 Feb 2007 09:32:26 +0000</pubDate>
		<guid isPermaLink="false">http://hexmen.com/blog/2006/05/revisiting-functionprototypeapply-for-ie5/#comment-46</guid>
		<description>Hi Márcio,

You could, but that still doesn&#039;t guarantee the property &lt;code&gt;methodName&lt;/code&gt; is unused (an &lt;code&gt;undefined&lt;/code&gt; value is perfectly legitimate.)

With modern browsers, you can check for the existence of an object property using &lt;code&gt;hasOwnProperty&lt;/code&gt;.  With your code, you&#039;d change the while-condition to &lt;code&gt;while (thisArg.hasOwnProperty(methodName))&lt;/code&gt;

However, IE5 doesn&#039;t support &lt;code&gt;hasOwnProperty&lt;/code&gt;, and I don&#039;t think there&#039;s a perfect fix for it.</description>
		<content:encoded><![CDATA[<p>Hi Márcio,</p>
<p>You could, but that still doesn&#8217;t guarantee the property <code>methodName</code> is unused (an <code>undefined</code> value is perfectly legitimate.)</p>
<p>With modern browsers, you can check for the existence of an object property using <code>hasOwnProperty</code>.  With your code, you&#8217;d change the while-condition to <code>while (thisArg.hasOwnProperty(methodName))</code></p>
<p>However, IE5 doesn&#8217;t support <code>hasOwnProperty</code>, and I don&#8217;t think there&#8217;s a perfect fix for it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
