<?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: Javascript Inheritance</title>
	<atom:link href="http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/</link>
	<description></description>
	<pubDate>Tue, 07 Oct 2008 02:52:13 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: M</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/#comment-720805</link>
		<dc:creator>M</dc:creator>
		<pubDate>Mon, 12 May 2008 09:01:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-720805</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'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 :).</p>]]></content:encoded>
	</item>
	<item>
		<title>By: abigdog</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/#comment-693692</link>
		<dc:creator>abigdog</dc:creator>
		<pubDate>Wed, 16 Apr 2008 12:46:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-693692</guid>
		<description>function inherit(descendant, parent) {  
    var sConstructor = parent.toString();  
    var aMatch = sConstructor.match( /\s*function (.*)\(/ );  
    if ( aMatch != null ) { descendant.prototype[aMatch[1]] = parent; }  
    for (var m in parent.prototype) {  
        // MY HUMBLE ADDITION
        if (typeof(parent.prototype[m]) == 'function') {
            descendant.prototype[aMatch[1]+'_'+m] = parent.prototype[m];
        }
        descendant.prototype[m] = parent.prototype[m];  
    }  
}

my humble addition allows parent methods to be called without using "apply".

if B extends A as in Mr. Fuecks examples, and "obj" is an instance of A, and "category" a virtual function, class A's version can be invoked on obj as "obj.A_category()" similar to the c++ syntax "obj-&#62;A::category()"</description>
		<content:encoded><![CDATA[<p>function inherit(descendant, parent) {<br />
    var sConstructor = parent.toString();<br />
    var aMatch = sConstructor.match( /\s*function (.*)\(/ );<br />
    if ( aMatch != null ) { descendant.prototype[aMatch[1]] = parent; }<br />
    for (var m in parent.prototype) {<br />
        // MY HUMBLE ADDITION<br />
        if (typeof(parent.prototype[m]) == &#8216;function&#8217;) {<br />
            descendant.prototype[aMatch[1]+&#8217;_'+m] = parent.prototype[m];<br />
        }<br />
        descendant.prototype[m] = parent.prototype[m];<br />
    }<br />
}</p>
<p>my humble addition allows parent methods to be called without using &#8220;apply&#8221;.</p>
<p>if B extends A as in Mr. Fuecks examples, and &#8220;obj&#8221; is an instance of A, and &#8220;category&#8221; a virtual function, class A&#8217;s version can be invoked on obj as &#8220;obj.A_category()&#8221; similar to the c++ syntax &#8220;obj-&gt;A::category()&#8221;</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Alexandru Matei</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/#comment-309874</link>
		<dc:creator>Alexandru Matei</dc:creator>
		<pubDate>Wed, 18 Jul 2007 09:25:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-309874</guid>
		<description>Hi,

Your code would look like this.
It's simple and good enough for my needs. 
As said, I'm happy that it doesn't need copyPrototype and is possible to call the base class method which has been overridden.

  &lt;pre&gt;&lt;code class="javascript"&gt;function Animal(type) 
      { 
            alert('Base class constructor'); 

	    this.species = type; 
	    
	     //this method doesn’t need to be called from the derived class!
             this.SayHello = SayHello;
             
             function SayHello()
             {
                 alert ("Base class SayHello() '"+this.species+"'");
             } 
	
	     
	     //must be overridden  in the derived class
     	     Animal.prototype.Category = function()
	     { 
	            alert('Base class Category() ' +this.species); 
	     }
   	 
 
	}
	
         
	  
		 
	function Dog(type) 
	{ 
            alert('Derived class constructor'); 

	    // Call the base class constructor 
            Animal.call (this,type); 
   
            //override method
  	    Dog.prototype.Category = function() 
	    { 
	        //call the base class version !
	        Animal.prototype.Category.call(this); 
	        alert("Derived class Category() " +this.species); 
	    }
     
    }
   &lt;/code&gt;&lt;/pre&gt;

   &lt;pre&gt;&lt;code class="javascript"&gt;var d = new Dog('dog'); 
   d.SayHello();
   d.Category();&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Your code would look like this.<br />
It&#8217;s simple and good enough for my needs.<br />
As said, I&#8217;m happy that it doesn&#8217;t need copyPrototype and is possible to call the base class method which has been overridden.</p>
<pre><code class="javascript">function Animal(type) 
      { 
            alert('Base class constructor'); 

	    this.species = type; 
	    
	     //this method doesn’t need to be called from the derived class!
             this.SayHello = SayHello;
             
             function SayHello()
             {
                 alert ("Base class SayHello() '"+this.species+"'");
             } 
	
	     
	     //must be overridden  in the derived class
     	     Animal.prototype.Category = function()
	     { 
	            alert('Base class Category() ' +this.species); 
	     }
   	 
 
	}
	
         
	  
		 
	function Dog(type) 
	{ 
            alert('Derived class constructor'); 

	    // Call the base class constructor 
            Animal.call (this,type); 
   
            //override method
  	    Dog.prototype.Category = function() 
	    { 
	        //call the base class version !
	        Animal.prototype.Category.call(this); 
	        alert("Derived class Category() " +this.species); 
	    }
     
    }
   </code></pre>
<pre><code class="javascript">var d = new Dog('dog'); 
   d.SayHello();
   d.Category();</code></pre>]]></content:encoded>
	</item>
	<item>
		<title>By: Alexandru Matei</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/#comment-309858</link>
		<dc:creator>Alexandru Matei</dc:creator>
		<pubDate>Wed, 18 Jul 2007 09:10:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-309858</guid>
		<description>Hi,

The single inheritance example below doesn't need copyPrototype.
It works fine.

And is possible to call the base class method which has been overridden.

&lt;pre&gt;&lt;code class="javascript"&gt;    function BaseClass(divID) 
    { 
               
        alert("  Base class constructor"); 
        this.divID=divID;
       
        
        //this method doesn't need to be called from the derived class!
        this.SayHello = SayHello;
        
        function SayHello()
        {
              alert ("Base class SayHello() '"+this.divID+"'");
        } 
   
  
     //this method must be overridden in the derived class
      BaseClass.prototype.SayGoodbye = function(arg) 
      { 
            alert("Base class SayGoodbye(): '"+this.divID+"' "+arg); 
      }                 
        
	          
    } 
   
   
   
    function DerivedClass(divID) 
    { 
   
        alert("  Derived class constructor"); 
        
       //call base class constructor          
        BaseClass.call (this,divID); 
          
         //override method 
        DerivedClass.prototype.SayGoodbye = function(arg) 
        { 
            //call the base class version !
                      
            BaseClass.prototype.SayGoodbye.call(this,arg); 
            alert("Derived class SayGoodbye(): '"+this.divID+"' "+arg); 
        } 
           
    } 

   var b = new BaseClass("base class"); 
   b.SayGoodbye(102);     
   b.SayHello(); 

    var d = new DerivedClass("derived class"); 
    d.SayGoodbye(102);     
    d.SayHello();&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>The single inheritance example below doesn&#8217;t need copyPrototype.<br />
It works fine.</p>
<p>And is possible to call the base class method which has been overridden.</p>
<pre><code class="javascript">    function BaseClass(divID) 
    { 
               
        alert("  Base class constructor"); 
        this.divID=divID;
       
        
        //this method doesn't need to be called from the derived class!
        this.SayHello = SayHello;
        
        function SayHello()
        {
              alert ("Base class SayHello() '"+this.divID+"'");
        } 
   
  
     //this method must be overridden in the derived class
      BaseClass.prototype.SayGoodbye = function(arg) 
      { 
            alert("Base class SayGoodbye(): '"+this.divID+"' "+arg); 
      }                 
        
	          
    } 
   
   
   
    function DerivedClass(divID) 
    { 
   
        alert("  Derived class constructor"); 
        
       //call base class constructor          
        BaseClass.call (this,divID); 
          
         //override method 
        DerivedClass.prototype.SayGoodbye = function(arg) 
        { 
            //call the base class version !
                      
            BaseClass.prototype.SayGoodbye.call(this,arg); 
            alert("Derived class SayGoodbye(): '"+this.divID+"' "+arg); 
        } 
           
    } 

   var b = new BaseClass("base class"); 
   b.SayGoodbye(102);     
   b.SayHello(); 

    var d = new DerivedClass("derived class"); 
    d.SayGoodbye(102);     
    d.SayHello();</code></pre>]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Crowley</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/#comment-42367</link>
		<dc:creator>Paul Crowley</dc:creator>
		<pubDate>Thu, 03 Aug 2006 16:00:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-42367</guid>
		<description>I've &lt;a href="http://www.lshift.net/blog/2006/08/03/subclassing-in-javascript-part-2" rel="nofollow"&gt;blogged about an alternate approach which seems considerably more elegant to me &lt;/a&gt;- I'd be interested to hear what you think!</description>
		<content:encoded><![CDATA[<p>I&#8217;ve <a href="http://www.lshift.net/blog/2006/08/03/subclassing-in-javascript-part-2" rel="nofollow">blogged about an alternate approach which seems considerably more elegant to me </a>- I&#8217;d be interested to hear what you think!</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Le</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/#comment-38444</link>
		<dc:creator>Alex Le</dc:creator>
		<pubDate>Tue, 18 Jul 2006 06:38:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-38444</guid>
		<description>Everyone, the above code of &lt;code&gt;copyPrototype()&lt;/code&gt; in the article will NOT work in Internet Explorer (6.0) but works just fine in Firefox up to 1.5.0.4 in this following case:

&lt;pre&gt;
&lt;code class='javascript'&gt;
function Animal () {//code goes here } // notice the space between Animal and the left paranthesis
function Dog (){  this.Animal(); }   // this will generate error in IE
copyPrototype( Dog, Animal); //
&lt;/code&gt;
&lt;/pre&gt;

The reason is that the regexp in &lt;code&gt;copyPrototype&lt;/code&gt; used to match the parent's constructor will also match the space after (and before) the constructor's name.  Hence instead of grabbing "Animal" only, the pattern will also grab "Animal " instead.  Firefox, however, resolve this just fine.  IE, on the other hand, will give out a nasty and almost unidentifiable result in the call &lt;code&gt;this.Animal()&lt;/code&gt; in the Dog's constructor.  This bug took me almost 2 days to slowly work through and discover.

So here is the new fix snippet of &lt;code&gt;copyPrototype()&lt;/code&gt; that will work with any type of function declarations:

&lt;pre&gt;
&lt;code class='javascript'&gt;
function copyPrototype(descendant, parent) {
    var sConstructor = parent.toString();
    var aMatch = sConstructor.match( /\s*function (.*)\s*\(/ );
	
    var sConstructor = parent.toString();
    var aMatch = sConstructor.match( /\s*function (.*)\(/ );	
	if ( aMatch != null ) { descendant.prototype[aMatch[1].replace(/^\s*&#124;\s*$/g,"")] = parent; } 
		descendant.prototype[ aMatch[1] ] = parent; 
    for (var m in parent.prototype)
        descendant.prototype[m] = parent.prototype[m];
}
&lt;/code&gt;
&lt;/pre&gt;

What I have added here is 

&lt;code&gt;aMatch[1].replace(/^\s*&#124;\s*$/g,"")&lt;/code&gt; to trim the space before and after function name before the assignment.  This updated code has proved to work well in my test cases.

Cheers!

Alex Le at www.alexle.net</description>
		<content:encoded><![CDATA[<p>Everyone, the above code of <code>copyPrototype()</code> in the article will NOT work in Internet Explorer (6.0) but works just fine in Firefox up to 1.5.0.4 in this following case:</p>
<pre>
<code class='javascript'>
function Animal () {//code goes here } // notice the space between Animal and the left paranthesis
function Dog (){  this.Animal(); }   // this will generate error in IE
copyPrototype( Dog, Animal); //
</code>
</pre>
<p>The reason is that the regexp in <code>copyPrototype</code> used to match the parent&#8217;s constructor will also match the space after (and before) the constructor&#8217;s name.  Hence instead of grabbing &#8220;Animal&#8221; only, the pattern will also grab &#8220;Animal &#8221; instead.  Firefox, however, resolve this just fine.  IE, on the other hand, will give out a nasty and almost unidentifiable result in the call <code>this.Animal()</code> in the Dog&#8217;s constructor.  This bug took me almost 2 days to slowly work through and discover.</p>
<p>So here is the new fix snippet of <code>copyPrototype()</code> that will work with any type of function declarations:</p>
<pre>
<code class='javascript'>
function copyPrototype(descendant, parent) {
    var sConstructor = parent.toString();
    var aMatch = sConstructor.match( /\s*function (.*)\s*\(/ );
	
    var sConstructor = parent.toString();
    var aMatch = sConstructor.match( /\s*function (.*)\(/ );	
	if ( aMatch != null ) { descendant.prototype[aMatch[1].replace(/^\s*|\s*$/g,"")] = parent; } 
		descendant.prototype[ aMatch[1] ] = parent; 
    for (var m in parent.prototype)
        descendant.prototype[m] = parent.prototype[m];
}
</code>
</pre>
<p>What I have added here is </p>
<p><code>aMatch[1].replace(/^\s*|\s*$/g,"")</code> to trim the space before and after function name before the assignment.  This updated code has proved to work well in my test cases.</p>
<p>Cheers!</p>
<p>Alex Le at <a href="http://www.alexle.net" rel="nofollow">http://www.alexle.net</a></p>]]></content:encoded>
	</item>
	<item>
		<title>By: Twologic &#187; Blog Archive &#187; Javascript OO Ruby Style</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/#comment-31918</link>
		<dc:creator>Twologic &#187; Blog Archive &#187; Javascript OO Ruby Style</dc:creator>
		<pubDate>Thu, 22 Jun 2006 06:49:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-31918</guid>
		<description>[...] I want to achieve the above without resorting to global functions to build prototype chains [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] I want to achieve the above without resorting to global functions to build prototype chains [&#8230;]</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Twologic &#187; Blog Archive &#187; Ruby OO Continued&#8230;</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/#comment-30787</link>
		<dc:creator>Twologic &#187; Blog Archive &#187; Ruby OO Continued&#8230;</dc:creator>
		<pubDate>Sun, 18 Jun 2006 03:59:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-30787</guid>
		<description>[...] I want to achieve the above without resorting to global functions to build prototype chains [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] I want to achieve the above without resorting to global functions to build prototype chains [&#8230;]</p>]]></content:encoded>
	</item>
	<item>
		<title>By: wanderingken.com &#187; Extending Prototype : Class.extend()</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/#comment-16924</link>
		<dc:creator>wanderingken.com &#187; Extending Prototype : Class.extend()</dc:creator>
		<pubDate>Sat, 01 Apr 2006 19:25:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-16924</guid>
		<description>[...] If you&#8217;re like me, however, you&#8217;re still rather troubled by prototype&#8217;s claim of &#8220;class-driven development&#8221; in Javascript. In fact, you&#8217;re probably a little fuzzy on the differences between object-oriented and prototype-based languages (hey, is that why they called it prototype?), and, if you&#8217;ve investigated them, you find approaches to enabling classical object-oriented development in Javascript rather unintuitive. (See here, here, and here, all good stuff, but rather painful to use). [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] If you&#8217;re like me, however, you&#8217;re still rather troubled by prototype&#8217;s claim of &#8220;class-driven development&#8221; in Javascript. In fact, you&#8217;re probably a little fuzzy on the differences between object-oriented and prototype-based languages (hey, is that why they called it prototype?), and, if you&#8217;ve investigated them, you find approaches to enabling classical object-oriented development in Javascript rather unintuitive. (See here, here, and here, all good stuff, but rather painful to use). [&#8230;]</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Ajaxian &#187; Dean Edwards and Another Base.js</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/#comment-16404</link>
		<dc:creator>Ajaxian &#187; Dean Edwards and Another Base.js</dc:creator>
		<pubDate>Sat, 25 Mar 2006 00:56:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-16404</guid>
		<description>[...] I want to achieve the above without resorting to global functions to build prototype chains [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] I want to achieve the above without resorting to global functions to build prototype chains [&#8230;]</p>]]></content:encoded>
	</item>
</channel>
</rss>
