<?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: How to inherit classes in javascript</title>
	<atom:link href="http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Fri, 30 Jul 2010 18:10:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Michael</title>
		<link>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/comment-page-1/#comment-854</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Wed, 16 Dec 2009 10:30:17 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=41#comment-854</guid>
		<description>I observed one thing about Base2 and Prototype. When you call a chain method and you reach the base method, inside the method you have access to the derived object members using “this”. In my opinion once the call is navigating thru the chain on each base class the user should not be able to access the derived class members based on OOP definitions. I also tried a different approach of JavaScript inheritance, it can make use of closures or prototype, it’s up to the way you want to implement it http://dotnetcaffe.blogspot.com/2009/12/javascript-inheritance.html. In my case if the base class constructor requires parameters they will be passed from the derived class. Here is an example:

var Human = MG.Class.Create({

Gender: null,

constructor: function(gender) {

this.Gender = gender;

},

GenderString: function() {

return this.Gender == ‘F’ ? “female” : “male”;

},

ToOverride: function() { return “Called from Human”; }

}, null, null);

var Individual = MG.Class.Create({

Unique: null,

constructor: function(unique) {

this.Unique = unique;

},

IsUnique: function() {

return this.Unique ? “Yes” : “No”;

},

ToOverride: function() { return “Called from Individual which is ” + (this.Unique ? “unique” : “not unique”) + ” / ” + this.base(); }

}, Human, null);

var objHuman = new Human(”F”);

var objIndividual = new Individual(true, “M”);

In my opinion also a check for parameters of the base constructor should be added. If the derived object does not provide on instantiation the correct number or parameters for the base, then an exception should be triggered.</description>
		<content:encoded><![CDATA[<p>I observed one thing about Base2 and Prototype. When you call a chain method and you reach the base method, inside the method you have access to the derived object members using “this”. In my opinion once the call is navigating thru the chain on each base class the user should not be able to access the derived class members based on OOP definitions. I also tried a different approach of JavaScript inheritance, it can make use of closures or prototype, it’s up to the way you want to implement it <a href="http://dotnetcaffe.blogspot.com/2009/12/javascript-inheritance.html" rel="nofollow">http://dotnetcaffe.blogspot.com/2009/12/javascript-inheritance.html</a>. In my case if the base class constructor requires parameters they will be passed from the derived class. Here is an example:</p>
<p>var Human = MG.Class.Create({</p>
<p>Gender: null,</p>
<p>constructor: function(gender) {</p>
<p>this.Gender = gender;</p>
<p>},</p>
<p>GenderString: function() {</p>
<p>return this.Gender == ‘F’ ? “female” : “male”;</p>
<p>},</p>
<p>ToOverride: function() { return “Called from Human”; }</p>
<p>}, null, null);</p>
<p>var Individual = MG.Class.Create({</p>
<p>Unique: null,</p>
<p>constructor: function(unique) {</p>
<p>this.Unique = unique;</p>
<p>},</p>
<p>IsUnique: function() {</p>
<p>return this.Unique ? “Yes” : “No”;</p>
<p>},</p>
<p>ToOverride: function() { return “Called from Individual which is ” + (this.Unique ? “unique” : “not unique”) + ” / ” + this.base(); }</p>
<p>}, Human, null);</p>
<p>var objHuman = new Human(”F”);</p>
<p>var objIndividual = new Individual(true, “M”);</p>
<p>In my opinion also a check for parameters of the base constructor should be added. If the derived object does not provide on instantiation the correct number or parameters for the base, then an exception should be triggered.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shoaib Iqbal</title>
		<link>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/comment-page-1/#comment-848</link>
		<dc:creator>Shoaib Iqbal</dc:creator>
		<pubDate>Fri, 20 Nov 2009 03:49:52 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=41#comment-848</guid>
		<description>Nice work. Clear and Easy to understand.
Thanks</description>
		<content:encoded><![CDATA[<p>Nice work. Clear and Easy to understand.<br />
Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erwin Haantjes</title>
		<link>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/comment-page-1/#comment-803</link>
		<dc:creator>Erwin Haantjes</dc:creator>
		<pubDate>Mon, 05 Oct 2009 03:25:48 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=41#comment-803</guid>
		<description>It&#039;s very nice to have real OOP constructions in javascript. I have made a new event driven object model above the prototype objectmodel of javascript. There is one baseobject that implements this object model. It is a good method to reduce duplicated code (overhead) and to reduce download times when new functionality comes in. You can extend a class, it has a &quot;real&quot; constructor (__construct) and destructor (__destruct), alot of standard events (and other things such as ajax buffering e.a) and you are able to call inherited methods by simply calling &quot;this.inherited(arguments);&quot;.

Reusing code is a state of mind. Most programmers writing a solution to the problem only and do this over and over again for each project. Paste and copy is not the same, and also reinventing the wheel is not the same. Most solutions are based upon the same with a little difference. To write such *open* classes (and use it successfully) you must understand real OOP and what you really can do with it. The order and open construction of it is very important. Most of the javascript programmers/(especially)webdesigners doesn&#039;t have this challenge (sorry to say). It is not difficult but it is another way of thinking to develop robust solutions on the long term.

To give you any idea (because i don&#039;t give the code away at this moment), after the main class (class_object.js) is loaded you can do such things as:

function CMyClass( oData )
{
  var myProperty1 = &quot;Hello&quot;;
  var myProperty2 = &quot;World&quot;;

  objectExtend( this, &quot;CObject&quot;, arguments, oData ); // Extend it
}

CMyClass.prototype.__construct = function() // overwrite constructor
{
  this.inherited(arguments); // call inherited constructor CObject.__construct()
  this.installCss( &quot;mycss.css&quot;, &quot;css/&quot; );
}

CMyClass.prototype.installCss = function( asFileName, sBasepath, sMedia, bAppendToBody  )
{
  if( sMedia == undefined )
   { var sMedia = &quot;screen&quot;; }
  this.inherited(arguments);
}

 // Create the class
var oExample = new CMyClass({onCreate:function(oSender) {alert(&quot;created &quot;+this.className+this.myProperty1+&quot; &quot;+this.myProperty1);}});

When the object is created it will show an alertbox with the text &quot;CMyClass Hello World&quot; and loads the stylesheet css/mycss.css&quot;. NOTE: &quot;this&quot; is the object, because there is also a &quot;call in scope construction&quot; for events.

As you can see, it is possible to do!</description>
		<content:encoded><![CDATA[<p>It&#8217;s very nice to have real OOP constructions in javascript. I have made a new event driven object model above the prototype objectmodel of javascript. There is one baseobject that implements this object model. It is a good method to reduce duplicated code (overhead) and to reduce download times when new functionality comes in. You can extend a class, it has a &#8220;real&#8221; constructor (__construct) and destructor (__destruct), alot of standard events (and other things such as ajax buffering e.a) and you are able to call inherited methods by simply calling &#8220;this.inherited(arguments);&#8221;.</p>
<p>Reusing code is a state of mind. Most programmers writing a solution to the problem only and do this over and over again for each project. Paste and copy is not the same, and also reinventing the wheel is not the same. Most solutions are based upon the same with a little difference. To write such *open* classes (and use it successfully) you must understand real OOP and what you really can do with it. The order and open construction of it is very important. Most of the javascript programmers/(especially)webdesigners doesn&#8217;t have this challenge (sorry to say). It is not difficult but it is another way of thinking to develop robust solutions on the long term.</p>
<p>To give you any idea (because i don&#8217;t give the code away at this moment), after the main class (class_object.js) is loaded you can do such things as:</p>
<p>function CMyClass( oData )<br />
{<br />
  var myProperty1 = &#8220;Hello&#8221;;<br />
  var myProperty2 = &#8220;World&#8221;;</p>
<p>  objectExtend( this, &#8220;CObject&#8221;, arguments, oData ); // Extend it<br />
}</p>
<p>CMyClass.prototype.__construct = function() // overwrite constructor<br />
{<br />
  this.inherited(arguments); // call inherited constructor CObject.__construct()<br />
  this.installCss( &#8220;mycss.css&#8221;, &#8220;css/&#8221; );<br />
}</p>
<p>CMyClass.prototype.installCss = function( asFileName, sBasepath, sMedia, bAppendToBody  )<br />
{<br />
  if( sMedia == undefined )<br />
   { var sMedia = &#8220;screen&#8221;; }<br />
  this.inherited(arguments);<br />
}</p>
<p> // Create the class<br />
var oExample = new CMyClass({onCreate:function(oSender) {alert(&#8220;created &#8220;+this.className+this.myProperty1+&#8221; &#8220;+this.myProperty1);}});</p>
<p>When the object is created it will show an alertbox with the text &#8220;CMyClass Hello World&#8221; and loads the stylesheet css/mycss.css&#8221;. NOTE: &#8220;this&#8221; is the object, because there is also a &#8220;call in scope construction&#8221; for events.</p>
<p>As you can see, it is possible to do!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: M</title>
		<link>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/comment-page-1/#comment-473</link>
		<dc:creator>M</dc:creator>
		<pubDate>Mon, 12 May 2008 09:30:22 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=41#comment-473</guid>
		<description>Hi,
I have tried an different techniques related to JavaScript inheritance subject.
The code and the explanation are to long to post them here so if anyone is interested to take a look over it you can find it at &lt;a&gt;www.dotnetcaffe.net&lt;/a&gt; under JavaScript category. Fell free to criticize the code in any way you want...just don&#039;t flame :).</description>
		<content:encoded><![CDATA[<p>Hi,<br />
I have tried an different techniques related to JavaScript inheritance subject.<br />
The code and the explanation are to long to post them here so if anyone is interested to take a look over it you can find it at <a>http://www.dotnetcaffe.net</a> under JavaScript category. Fell free to criticize the code in any way you want&#8230;just don&#8217;t flame <img src='http://www.javascriptkata.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pedro Andujar</title>
		<link>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/comment-page-1/#comment-420</link>
		<dc:creator>Pedro Andujar</dc:creator>
		<pubDate>Fri, 25 Jan 2008 15:01:11 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=41#comment-420</guid>
		<description>Hello,
Sorry, I don&#039;t speak English very well.

JavaScript &quot;like Java&quot;.

var ClassA = Class.create(&quot;ClassA&quot;,function(){

	var value = &quot;value:A&quot;;
	this.getValue = function(){
		return value;
	};
	this.$$constructor = function(){
	};
	
});

var ClassB = Class.extend(&quot;ClassB&quot;,ClassA,function(){

	var value = &quot;value:B&quot;;
	this.getValue = function(){
		return value + &quot; &quot; + this.$$super[0].getValue();
	};
	this.$$constructor = function(){
	};
	
});

var instB = new ClassB();
alert(instB.getValue());// value:B value:A

You can see more examples in http://www.jsimpleclass.net/ 

Bye</description>
		<content:encoded><![CDATA[<p>Hello,<br />
Sorry, I don&#8217;t speak English very well.</p>
<p>JavaScript &#8220;like Java&#8221;.</p>
<p>var ClassA = Class.create(&#8220;ClassA&#8221;,function(){</p>
<p>	var value = &#8220;value:A&#8221;;<br />
	this.getValue = function(){<br />
		return value;<br />
	};<br />
	this.$$constructor = function(){<br />
	};</p>
<p>});</p>
<p>var ClassB = Class.extend(&#8220;ClassB&#8221;,ClassA,function(){</p>
<p>	var value = &#8220;value:B&#8221;;<br />
	this.getValue = function(){<br />
		return value + &#8221; &#8221; + this.$$super[0].getValue();<br />
	};<br />
	this.$$constructor = function(){<br />
	};</p>
<p>});</p>
<p>var instB = new ClassB();<br />
alert(instB.getValue());// value:B value:A</p>
<p>You can see more examples in <a href="http://www.jsimpleclass.net/" rel="nofollow">http://www.jsimpleclass.net/</a> </p>
<p>Bye</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/comment-page-1/#comment-265</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Fri, 15 Jun 2007 10:24:50 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=41#comment-265</guid>
		<description>PROBLEM WITH THIS APPROACH: The problem arises when a closure associated with the object instance is being used to provide private instance members because assigning an instance of that object to the prototype of another constructor will only result in one closure being formed. The privileged methods of the object instance on the prototype are &quot;inherited&quot; by objects created with the constructor but they all access the same, single, closure.

SOLUTION: Parasitic inheritance using Douglas Crockford&#039;s object function and Power Constructor pattern. See Advanced JavaScript video 1 at http://developer.yahoo.com/yui/theater/</description>
		<content:encoded><![CDATA[<p>PROBLEM WITH THIS APPROACH: The problem arises when a closure associated with the object instance is being used to provide private instance members because assigning an instance of that object to the prototype of another constructor will only result in one closure being formed. The privileged methods of the object instance on the prototype are &#8220;inherited&#8221; by objects created with the constructor but they all access the same, single, closure.</p>
<p>SOLUTION: Parasitic inheritance using Douglas Crockford&#8217;s object function and Power Constructor pattern. See Advanced JavaScript video 1 at <a href="http://developer.yahoo.com/yui/theater/" rel="nofollow">http://developer.yahoo.com/yui/theater/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BK</title>
		<link>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/comment-page-1/#comment-264</link>
		<dc:creator>BK</dc:creator>
		<pubDate>Tue, 29 May 2007 16:36:06 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=41#comment-264</guid>
		<description>I&#039;m quite sure it would work but the trap here is for maintenance. You are much likely to forget to change one of your &quot;base-class-call&quot; if anything in your &quot;object hierarchy&quot; change. If you use the .base approach, you change one place and everything else will work as expected.

But I&#039;m quite sure that your approach is closer to a prototypal approach.</description>
		<content:encoded><![CDATA[<p>I&#8217;m quite sure it would work but the trap here is for maintenance. You are much likely to forget to change one of your &#8220;base-class-call&#8221; if anything in your &#8220;object hierarchy&#8221; change. If you use the .base approach, you change one place and everything else will work as expected.</p>
<p>But I&#8217;m quite sure that your approach is closer to a prototypal approach.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/comment-page-1/#comment-263</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Tue, 29 May 2007 15:55:10 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=41#comment-263</guid>
		<description>Calling a super-class implementation...

Won&#039;t something like,

Mammal.Run.call(catInstance);

do the trick. Or if you&#039;re in a Cat instance (and say want to extend the super-class)

Cat.prototype.Run = function()
{
  Mammal.Run.call(this);
  // Do more stuff
}</description>
		<content:encoded><![CDATA[<p>Calling a super-class implementation&#8230;</p>
<p>Won&#8217;t something like,</p>
<p>Mammal.Run.call(catInstance);</p>
<p>do the trick. Or if you&#8217;re in a Cat instance (and say want to extend the super-class)</p>
<p>Cat.prototype.Run = function()<br />
{<br />
  Mammal.Run.call(this);<br />
  // Do more stuff<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/comment-page-1/#comment-262</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Fri, 25 May 2007 20:15:05 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=41#comment-262</guid>
		<description>@Luis
I don&#039;t want to start the debate either and I didn&#039;t take it personal. It&#039;s just that you are right on the point to stop thinking of javascript as &quot;it should do as OO&quot;.

I you ever write something on mixin, send me a mail and I&#039;ll try to link to it in a elegant way!

Thanks</description>
		<content:encoded><![CDATA[<p>@Luis<br />
I don&#8217;t want to start the debate either and I didn&#8217;t take it personal. It&#8217;s just that you are right on the point to stop thinking of javascript as &#8220;it should do as OO&#8221;.</p>
<p>I you ever write something on mixin, send me a mail and I&#8217;ll try to link to it in a elegant way!</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luis' Parenthesis</title>
		<link>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/comment-page-1/#comment-261</link>
		<dc:creator>Luis' Parenthesis</dc:creator>
		<pubDate>Fri, 25 May 2007 20:02:58 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=41#comment-261</guid>
		<description>I thank you for your article, Dan.  I really think it can be helpful to beginners.  Please don&#039;t take my comment too personal.  I merely intended to point out that there might be a slightly different, more natural way to envision JS than through inheritance hierarchies.

I still haven&#039;t completely exploited the full potential of mixins, but I think it&#039;s a path worth exploring...  Perhaps I should write about this somewhere to illustrate what I have in mind and stimulate conversation...

Finally, I just want to clear out that I didn&#039;t intend to start a debate about OO vs. prototyping, but rather a discussion about how we can best marry the two.</description>
		<content:encoded><![CDATA[<p>I thank you for your article, Dan.  I really think it can be helpful to beginners.  Please don&#8217;t take my comment too personal.  I merely intended to point out that there might be a slightly different, more natural way to envision JS than through inheritance hierarchies.</p>
<p>I still haven&#8217;t completely exploited the full potential of mixins, but I think it&#8217;s a path worth exploring&#8230;  Perhaps I should write about this somewhere to illustrate what I have in mind and stimulate conversation&#8230;</p>
<p>Finally, I just want to clear out that I didn&#8217;t intend to start a debate about OO vs. prototyping, but rather a discussion about how we can best marry the two.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
