<?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: Prototype Chain</title>
	<atom:link href="http://itsalleasy.com/2006/02/05/prototype-chain/feed/" rel="self" type="application/rss+xml" />
	<link>http://itsalleasy.com/2006/02/05/prototype-chain/</link>
	<description>...how boring would *that* be</description>
	<pubDate>Thu, 20 Nov 2008 15:13:29 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: james</title>
		<link>http://itsalleasy.com/2006/02/05/prototype-chain/#comment-88</link>
		<dc:creator>james</dc:creator>
		<pubDate>Thu, 09 Aug 2007 00:24:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.itsalleasy.com/2006/02/05/prototype-chain/#comment-88</guid>
		<description>Awesome, thanks.  I've also seen it crop up in the YUI framework.  I'm glad to see that other, smarter folks have found the same approach.</description>
		<content:encoded><![CDATA[<p>Awesome, thanks.  I&#8217;ve also seen it crop up in the YUI framework.  I&#8217;m glad to see that other, smarter folks have found the same approach.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://itsalleasy.com/2006/02/05/prototype-chain/#comment-87</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Wed, 08 Aug 2007 19:03:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.itsalleasy.com/2006/02/05/prototype-chain/#comment-87</guid>
		<description>That's actually similar to a function within Google Maps (renamed to help me figure out what's going on):

function aExtendsB( a,b ) {
	var c = function() {};
	c.prototype = b.prototype;
	a.prototype = new c
}</description>
		<content:encoded><![CDATA[<p>That&#8217;s actually similar to a function within Google Maps (renamed to help me figure out what&#8217;s going on):</p>
<p>function aExtendsB( a,b ) {<br />
	var c = function() {};<br />
	c.prototype = b.prototype;<br />
	a.prototype = new c<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://itsalleasy.com/2006/02/05/prototype-chain/#comment-86</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Mon, 06 Aug 2007 20:10:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.itsalleasy.com/2006/02/05/prototype-chain/#comment-86</guid>
		<description>Whoah! Thanks!!

I've just spent the whole day struggling with this exact problem where
"ChildClass.prototype = new ParentClass();"
involves an expensive constructor which relies on the DOM and other global variables.

Hopefully your code will set things right.

Does this depend on a library (prototype.js, mootools.js etc) for Class?  A library-independant example would be nice.</description>
		<content:encoded><![CDATA[<p>Whoah! Thanks!!</p>
<p>I&#8217;ve just spent the whole day struggling with this exact problem where<br />
&#8220;ChildClass.prototype = new ParentClass();&#8221;<br />
involves an expensive constructor which relies on the DOM and other global variables.</p>
<p>Hopefully your code will set things right.</p>
<p>Does this depend on a library (prototype.js, mootools.js etc) for Class?  A library-independant example would be nice.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: james</title>
		<link>http://itsalleasy.com/2006/02/05/prototype-chain/#comment-85</link>
		<dc:creator>james</dc:creator>
		<pubDate>Wed, 22 Mar 2006 19:52:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.itsalleasy.com/2006/02/05/prototype-chain/#comment-85</guid>
		<description>No, that is no typo...understandably confusing though ;)  That line is 'fixing' the 'constructor' property for all instances of the sub-'class'.  Without that line, the following would occur:
var sub = new SubClass();
alert(sub.constructor == SubClass); //alerts FALSE
alert(sub.constructor == SuperClass); //also FALSE since it really is the anonymous inner function

if the prototype.constructor was set to SuperClass, then you get the following:
var sub = new SubClass();
alert(sub.constructor == SubClass); //alerts FALSE
alert(sub.constructor == SuperClass); //TRUE, but should it be?

Basically, what is happening is that when the function is created by the engine, it is given a prototype property...and it holds a property 'constructor' by default that points to the original fn the prototype was created for.  That way, all instances of that 'class' automatically have a 'constructor' prop that points to the original constructor function.  When the prototype is set to an instance of a different 'class', the prototype for the original constructor function then becomes that instance...which has a constructor prop that points to ITS original constructor function.  So we just need to change the constructor prop for that instance to be the SubClass function...since it is being used as the SubClasses prototype this should be fine.

The only headaches this really causes is when you need to programmatically traverse the prototype chain.  For this purpose, the superClass prop was added.  BTW, this problem would exist even in the classical "SubClass.prototype = new SuperClass()" approach.

The only other way to get the prototype fixed would be to actually set the 'constructor' prop on every instance.  That would require all instances be created via a factory method or something similar.  It would be doable if there was like a magic _new property of every function that got called for every instance...then you could say:
SubClass._new = function(){
   this.constructor = SubClass;
}
And in that case, you would definitely want to set SubClass.prototype.constructor to SuperClass...and then prototype traversal wouldn't need a 'superClass' prop.

Sorry for the long-winded answer...I know its rather confusing.  Hope I made it more clear.</description>
		<content:encoded><![CDATA[<p>No, that is no typo&#8230;understandably confusing though <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  That line is &#8216;fixing&#8217; the &#8216;constructor&#8217; property for all instances of the sub-&#8217;class&#8217;.  Without that line, the following would occur:<br />
var sub = new SubClass();<br />
alert(sub.constructor == SubClass); //alerts FALSE<br />
alert(sub.constructor == SuperClass); //also FALSE since it really is the anonymous inner function</p>
<p>if the prototype.constructor was set to SuperClass, then you get the following:<br />
var sub = new SubClass();<br />
alert(sub.constructor == SubClass); //alerts FALSE<br />
alert(sub.constructor == SuperClass); //TRUE, but should it be?</p>
<p>Basically, what is happening is that when the function is created by the engine, it is given a prototype property&#8230;and it holds a property &#8216;constructor&#8217; by default that points to the original fn the prototype was created for.  That way, all instances of that &#8216;class&#8217; automatically have a &#8216;constructor&#8217; prop that points to the original constructor function.  When the prototype is set to an instance of a different &#8216;class&#8217;, the prototype for the original constructor function then becomes that instance&#8230;which has a constructor prop that points to ITS original constructor function.  So we just need to change the constructor prop for that instance to be the SubClass function&#8230;since it is being used as the SubClasses prototype this should be fine.</p>
<p>The only headaches this really causes is when you need to programmatically traverse the prototype chain.  For this purpose, the superClass prop was added.  BTW, this problem would exist even in the classical &#8220;SubClass.prototype = new SuperClass()&#8221; approach.</p>
<p>The only other way to get the prototype fixed would be to actually set the &#8216;constructor&#8217; prop on every instance.  That would require all instances be created via a factory method or something similar.  It would be doable if there was like a magic _new property of every function that got called for every instance&#8230;then you could say:<br />
SubClass._new = function(){<br />
   this.constructor = SubClass;<br />
}<br />
And in that case, you would definitely want to set SubClass.prototype.constructor to SuperClass&#8230;and then prototype traversal wouldn&#8217;t need a &#8217;superClass&#8217; prop.</p>
<p>Sorry for the long-winded answer&#8230;I know its rather confusing.  Hope I made it more clear.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Geary</title>
		<link>http://itsalleasy.com/2006/02/05/prototype-chain/#comment-84</link>
		<dc:creator>Michael Geary</dc:creator>
		<pubDate>Wed, 22 Mar 2006 19:26:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.itsalleasy.com/2006/02/05/prototype-chain/#comment-84</guid>
		<description>Very interesting and elegant solution!

I'm scratching my head over one line of code, though. The last line is:

   SubClass.prototype.constructor = SubClass;

For some reason I'm thinking that should be:

   SubClass.prototype.constructor = SuperClass;

Was that a typo, or am I mixed up? (The latter is quite possible...)

Thanks!</description>
		<content:encoded><![CDATA[<p>Very interesting and elegant solution!</p>
<p>I&#8217;m scratching my head over one line of code, though. The last line is:</p>
<p>   SubClass.prototype.constructor = SubClass;</p>
<p>For some reason I&#8217;m thinking that should be:</p>
<p>   SubClass.prototype.constructor = SuperClass;</p>
<p>Was that a typo, or am I mixed up? (The latter is quite possible&#8230;)</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: troels</title>
		<link>http://itsalleasy.com/2006/02/05/prototype-chain/#comment-83</link>
		<dc:creator>troels</dc:creator>
		<pubDate>Wed, 08 Feb 2006 15:32:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.itsalleasy.com/2006/02/05/prototype-chain/#comment-83</guid>
		<description>That is very clever indeed. Not only does the instanceof operator work as expected, but if you add a method to the superclass' prototype, it appears on instances of the childclass - sweet!</description>
		<content:encoded><![CDATA[<p>That is very clever indeed. Not only does the instanceof operator work as expected, but if you add a method to the superclass&#8217; prototype, it appears on instances of the childclass - sweet!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
