<?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: On modifying prototypes of JavaScript built-ins</title>
	<atom:link href="http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/feed/" rel="self" type="application/rss+xml" />
	<link>http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/</link>
	<description>Web development concerns, usually revolving around implimentation of designs into graphics, CSS, and HTML.</description>
	<pubDate>Thu, 24 Jul 2008 06:47:23 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: Daniel Phillimore</title>
		<link>http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-2384</link>
		<dc:creator>Daniel Phillimore</dc:creator>
		<pubDate>Sun, 19 Nov 2006 00:15:33 +0000</pubDate>
		<guid isPermaLink="false">http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-2384</guid>
		<description>In response to that last post, all I do to skip extended members is as follows (untested, conceptual):

Array.prototype.extender = window.alert; //random reference
var arrArray = ["foo", "bar", "chicken"];
for (var anyIndex in arrArray)
{
    //skip extension
    if (anyIndex != "extender") continue;
   
    //...

    //do_something...
}</description>
		<content:encoded><![CDATA[<p>In response to that last post, all I do to skip extended members is as follows (untested, conceptual):</p>
<p>Array.prototype.extender = window.alert; //random reference<br />
var arrArray = [&#8221;foo&#8221;, &#8220;bar&#8221;, &#8220;chicken&#8221;];<br />
for (var anyIndex in arrArray)<br />
{<br />
    //skip extension<br />
    if (anyIndex != &#8220;extender&#8221;) continue;</p>
<p>    //&#8230;</p>
<p>    //do_something&#8230;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thomas  Frank</title>
		<link>http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-304</link>
		<dc:creator>Thomas  Frank</dc:creator>
		<pubDate>Fri, 16 Jun 2006 02:57:08 +0000</pubDate>
		<guid isPermaLink="false">http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-304</guid>
		<description>Sorry didn't get my pre tags right ;-):

&lt;code&gt;
//let's extend another prototype
String.prototype.proto=function(obj){
	var proto=obj.sort?Array.prototype:Object.prototype;
	return proto[this] &#38;&#38; proto[this]==obj[this]
};


// and extend some more prototypes
Object.prototype.you=1;
Array.prototype._dont=1;
Object.prototype.like=1;
Array.prototype._this=1;

// and create a new array
var x=[1,1,1];
x.you=2;


// now it is really easy to iterate through non-prototype members
// this will return 0,1,2,you
props=[];
for(var i in x){
	if(!i.proto(x)){
		props.push(i)
	}
}; 
alert(props.join("\n"))
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Sorry didn&#8217;t get my pre tags right ;-):</p>
<p><code><br />
//let's extend another prototype<br />
String.prototype.proto=function(obj){<br />
	var proto=obj.sort?Array.prototype:Object.prototype;<br />
	return proto[this] &amp;&amp; proto[this]==obj[this]<br />
};</p>
<p>// and extend some more prototypes<br />
Object.prototype.you=1;<br />
Array.prototype._dont=1;<br />
Object.prototype.like=1;<br />
Array.prototype._this=1;</p>
<p>// and create a new array<br />
var x=[1,1,1];<br />
x.you=2;</p>
<p>// now it is really easy to iterate through non-prototype members<br />
// this will return 0,1,2,you<br />
props=[];<br />
for(var i in x){<br />
	if(!i.proto(x)){<br />
		props.push(i)<br />
	}<br />
};<br />
alert(props.join("\n"))<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thomas  Frank</title>
		<link>http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-303</link>
		<dc:creator>Thomas  Frank</dc:creator>
		<pubDate>Fri, 16 Jun 2006 02:54:56 +0000</pubDate>
		<guid isPermaLink="false">http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-303</guid>
		<description>I really disagree too. Prototypes are an important part of JavaScript. It's not the programmer who extends them that is sloppy - it's the programmer who won't check if they're extended. It is really easy to check this:

//let's extend another prototype
String.prototype.proto=function(obj){
	var proto=obj.sort?Array.prototype:Object.prototype;
	return proto[this] &#38;&#38; proto[this]==obj[this]
};


// and extend some more prototypes
Object.prototype.you=1;
Array.prototype._dont=1;
Object.prototype.like=1;
Array.prototype._this=1;

// and create a new array
var x=[1,1,1];
x.you=2;


// now it is really easy to iterate through non-prototype members
// this will return 0,1,2,you
props=[];
for(var i in x){
	if(!i.proto(x)){
		props.push(i)
	}
}; 
alert(props.join("\n"))</description>
		<content:encoded><![CDATA[<p>I really disagree too. Prototypes are an important part of JavaScript. It&#8217;s not the programmer who extends them that is sloppy - it&#8217;s the programmer who won&#8217;t check if they&#8217;re extended. It is really easy to check this:</p>
<p>//let&#8217;s extend another prototype<br />
String.prototype.proto=function(obj){<br />
	var proto=obj.sort?Array.prototype:Object.prototype;<br />
	return proto[this] &amp;&amp; proto[this]==obj[this]<br />
};</p>
<p>// and extend some more prototypes<br />
Object.prototype.you=1;<br />
Array.prototype._dont=1;<br />
Object.prototype.like=1;<br />
Array.prototype._this=1;</p>
<p>// and create a new array<br />
var x=[1,1,1];<br />
x.you=2;</p>
<p>// now it is really easy to iterate through non-prototype members<br />
// this will return 0,1,2,you<br />
props=[];<br />
for(var i in x){<br />
	if(!i.proto(x)){<br />
		props.push(i)<br />
	}<br />
};<br />
alert(props.join(&#8221;\n&#8221;))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-279</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Mon, 12 Jun 2006 14:50:55 +0000</pubDate>
		<guid isPermaLink="false">http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-279</guid>
		<description>You wrote:
"using a normal for(i=0,l=a.length;0&#62;l;i++) loop"

Shouldn't this be
for(i=0,l=a.length;l&#62;i;i++) 

??</description>
		<content:encoded><![CDATA[<p>You wrote:<br />
&#8220;using a normal for(i=0,l=a.length;0&gt;l;i++) loop&#8221;</p>
<p>Shouldn&#8217;t this be<br />
for(i=0,l=a.length;l&gt;i;i++) </p>
<p>??</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dean Edwards</title>
		<link>http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-273</link>
		<dc:creator>Dean Edwards</dc:creator>
		<pubDate>Fri, 09 Jun 2006 11:04:23 +0000</pubDate>
		<guid isPermaLink="false">http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-273</guid>
		<description>I really disagree with this post. It is arguments like this that hold back JS development. None of the reasons you come up with have convinced me that extending the Array object is a bad idea.

In one of your code samples you use the example of Array.push. IE5.0 does not support this. How do we get our code to work with IE5.0? We extend the Array object of course.</description>
		<content:encoded><![CDATA[<p>I really disagree with this post. It is arguments like this that hold back JS development. None of the reasons you come up with have convinced me that extending the Array object is a bad idea.</p>
<p>In one of your code samples you use the example of Array.push. IE5.0 does not support this. How do we get our code to work with IE5.0? We extend the Array object of course.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Dupont</title>
		<link>http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-266</link>
		<dc:creator>Andrew Dupont</dc:creator>
		<pubDate>Thu, 08 Jun 2006 20:57:54 +0000</pubDate>
		<guid isPermaLink="false">http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-266</guid>
		<description>Liorean, this is a good post, and I wish I had happened upon it sooner.  I started to post a comment, but it turned into a &lt;a href="http://www.andrewdupont.net/2006/06/08/more-on-javascript-prototypes-and-forin/" rel="nofollow"&gt;theological tract&lt;/a&gt;, so I'll just link it here.  Cheers.</description>
		<content:encoded><![CDATA[<p>Liorean, this is a good post, and I wish I had happened upon it sooner.  I started to post a comment, but it turned into a <a href="http://www.andrewdupont.net/2006/06/08/more-on-javascript-prototypes-and-forin/" rel="nofollow">theological tract</a>, so I&#8217;ll just link it here.  Cheers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hermann Klinke</title>
		<link>http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-248</link>
		<dc:creator>Hermann Klinke</dc:creator>
		<pubDate>Wed, 31 May 2006 20:18:44 +0000</pubDate>
		<guid isPermaLink="false">http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-248</guid>
		<description>I see your point. Thanks for the clarification!</description>
		<content:encoded><![CDATA[<p>I see your point. Thanks for the clarification!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: liorean</title>
		<link>http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-240</link>
		<dc:creator>liorean</dc:creator>
		<pubDate>Tue, 30 May 2006 21:36:04 +0000</pubDate>
		<guid isPermaLink="false">http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-240</guid>
		<description>&lt;p&gt;Hermann: Yes, you can. But you're repeating the error I talked about with regard to the Array prototype, but in larger scale - by extending &lt;code&gt;Object.prototype&lt;/code&gt; you are forcing everybody else to do that runtime checking for every object whose members they want to iterate through. You're breaking all code that didn't do that checking because they knew that all built-in members are marked {DontEnum}. Breaking language built-ins is bad, because you're breaking code that normally would work perfectly fine. Any solution to the problem that would demand changing other code is unacceptable.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Hermann: Yes, you can. But you&#8217;re repeating the error I talked about with regard to the Array prototype, but in larger scale - by extending <code>Object.prototype</code> you are forcing everybody else to do that runtime checking for every object whose members they want to iterate through. You&#8217;re breaking all code that didn&#8217;t do that checking because they knew that all built-in members are marked {DontEnum}. Breaking language built-ins is bad, because you&#8217;re breaking code that normally would work perfectly fine. Any solution to the problem that would demand changing other code is unacceptable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hermann Klinke</title>
		<link>http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-237</link>
		<dc:creator>Hermann Klinke</dc:creator>
		<pubDate>Tue, 30 May 2006 21:06:22 +0000</pubDate>
		<guid isPermaLink="false">http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-237</guid>
		<description>I do not understand you problem with extending Object.prototype. You can still iterate over its members as you did before by ignoring the members of Object.prototype.

for (member in object)
{
   if (Object.prototype[member] === undefined)
   {
      //do your thing
   }
}</description>
		<content:encoded><![CDATA[<p>I do not understand you problem with extending Object.prototype. You can still iterate over its members as you did before by ignoring the members of Object.prototype.</p>
<p>for (member in object)<br />
{<br />
   if (Object.prototype[member] === undefined)<br />
   {<br />
      //do your thing<br />
   }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: liorean</title>
		<link>http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-219</link>
		<dc:creator>liorean</dc:creator>
		<pubDate>Thu, 25 May 2006 21:43:07 +0000</pubDate>
		<guid isPermaLink="false">http://web-graphics.com/2006/05/23/on-modifying-prototypes-of-javascript-built-ins/#comment-219</guid>
		<description>&lt;p&gt;Erik: It should only appear in an enumeration if it was overridden &#8212; which is the case that bug is about. But I'm talking about the case where it's not overridden - in that case, it will not be enumerated, because ECMA-262 3ed. specifies that it's {DontEnum}. So, there's no way of autodiscovering it in code. The problem is one of getting the property from the prototype when you in fact only wanted properties from the actual object. And for that, you only need to check whether it exists on the object at hand or not, which can be done using &lt;code&gt;arr.hasOwnProperty(key)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;As for sparse arrays not being enough of a concern to avoid extending &lt;code&gt;Array.prototype&lt;/code&gt; &#8212; maybe you're right about that. It's not a common use of arrays. I'm still of the opinion that libraries should try not to destroy native language functionality though.&lt;/p&gt;
&lt;p&gt;It's not as if arrays and iterating over them is homogenously implemented anyways. Try this in moz, iew and op:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;var
    a=[,,,,,,,,],
    b=[],
    i;
a[4]='first';
a[0]='second';
a[2]='third';
for(i in a)
   b.push(i+':'+a[i]);
p.push('length:'+a.length);
b.join();&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Moz and iew have their bugs with sparse array literals. Op has a different &#8212; but entirely correct accoring to the spec &#8212; implementation of the iteration through array members. Or rather of how those members are stored, but it amounts to a difference in the iteration.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Erik: It should only appear in an enumeration if it was overridden &mdash; which is the case that bug is about. But I&#8217;m talking about the case where it&#8217;s not overridden - in that case, it will not be enumerated, because ECMA-262 3ed. specifies that it&#8217;s {DontEnum}. So, there&#8217;s no way of autodiscovering it in code. The problem is one of getting the property from the prototype when you in fact only wanted properties from the actual object. And for that, you only need to check whether it exists on the object at hand or not, which can be done using <code>arr.hasOwnProperty(key)</code>.</p>
<p>As for sparse arrays not being enough of a concern to avoid extending <code>Array.prototype</code> &mdash; maybe you&#8217;re right about that. It&#8217;s not a common use of arrays. I&#8217;m still of the opinion that libraries should try not to destroy native language functionality though.</p>
<p>It&#8217;s not as if arrays and iterating over them is homogenously implemented anyways. Try this in moz, iew and op:</p>
<pre><code>var
    a=[,,,,,,,,],
    b=[],
    i;
a[4]='first';
a[0]='second';
a[2]='third';
for(i in a)
   b.push(i+':'+a[i]);
p.push('length:'+a.length);
b.join();</code></pre>
<p>Moz and iew have their bugs with sparse array literals. Op has a different &mdash; but entirely correct accoring to the spec &mdash; implementation of the iteration through array members. Or rather of how those members are stored, but it amounts to a difference in the iteration.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
