<?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: 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>News, opinion, and fresh thinking for web developers and designers. The official podcast of sitepoint.com.</description>
	<lastBuildDate>Sun, 08 Nov 2009 18:39:36 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Arunoda Susiripala</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/comment-page-1/#comment-896558</link>
		<dc:creator>Arunoda Susiripala</dc:creator>
		<pubDate>Wed, 18 Mar 2009 14:49:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-896558</guid>
		<description>&lt;strong&gt;A new Way of copyPrototype method&lt;/strong&gt;&lt;code&gt;

hello when i&#039;m reading this document i was clueless about the copyProtype method..because it&#039;s really hard to get the meaning of that...

So i&#039;ve remodified it with just 2 lines of codes with a more robustness...
&lt;/code&gt;&lt;code&gt;
function inherit(Child,Parent){
	Child.prototype=new Parent();
	Child.prototype[&#039;super&#039;]=Parent;
}
&lt;/code&gt;


Now what you&#039;ve to do is just call &lt;strong&gt;inherit&lt;/strong&gt; method and call this.super() in your Child class....

here is complete solution....
&lt;code&gt;
function inherit(Child,Parent){
	Child.prototype=new Parent();
	Child.prototype[&#039;super&#039;]=Parent;
}

function Phone(no){
	this.no=no;
	this.callTo=function(oNo){
		alert(&quot;calling &quot; + this.no + &quot; to &quot; + oNo );
	}
}

function VideoPhone(no){
	this.super(no);
	this.no=&quot;Video: &quot; + this.no;
}

inherit(VideoPhone,Phone);

var phone=new VideoPhone(&quot;0718360974&quot;);
phone.callTo(&quot;0098&quot;);

&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p><strong>A new Way of copyPrototype method</strong><code>

hello when i'm reading this document i was clueless about the copyProtype method..because it's really hard to get the meaning of that...

So i've remodified it with just 2 lines of codes with a more robustness...
</code><code>
function inherit(Child,Parent){
	Child.prototype=new Parent();
	Child.prototype['super']=Parent;
}
</code></p>
<p>Now what you&#8217;ve to do is just call <strong>inherit</strong> method and call this.super() in your Child class&#8230;.</p>
<p>here is complete solution&#8230;.<br />
<code>
function inherit(Child,Parent){
	Child.prototype=new Parent();
	Child.prototype['super']=Parent;
}

function Phone(no){
	this.no=no;
	this.callTo=function(oNo){
		alert("calling " + this.no + " to " + oNo );
	}
}

function VideoPhone(no){
	this.super(no);
	this.no="Video: " + this.no;
}

inherit(VideoPhone,Phone);

var phone=new VideoPhone("0718360974");
phone.callTo("0098");

</code></p>]]></content:encoded>
	</item>
	<item>
		<title>By: Arunoda Susiripala</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/comment-page-1/#comment-896554</link>
		<dc:creator>Arunoda Susiripala</dc:creator>
		<pubDate>Wed, 18 Mar 2009 14:46:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-896554</guid>
		<description>&lt;strong&gt;A new Way of copyPrototype method&lt;/strong&gt;&lt;code&gt;

hello when i&#039;m reading this document i was clueless about the copyProtype method..because it&#039;s really hard to get the meaning of that...

So i&#039;ve remodified it with just 2 lines of codes with a more robustness...

function inherit(Child,Parent){
Child.prototype=new Parent();
	Child.prototype[&#039;super&#039;]=Parent;
}
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p><strong>A new Way of copyPrototype method</strong><code>

hello when i'm reading this document i was clueless about the copyProtype method..because it's really hard to get the meaning of that...

So i've remodified it with just 2 lines of codes with a more robustness...

function inherit(Child,Parent){
Child.prototype=new Parent();
	Child.prototype['super']=Parent;
}
</code><code></code></p>]]></content:encoded>
	</item>
	<item>
		<title>By: Nisha</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/comment-page-1/#comment-842766</link>
		<dc:creator>Nisha</dc:creator>
		<pubDate>Tue, 02 Dec 2008 13:16:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1382#comment-842766</guid>
		<description>Such a helpful link for javascript inheritance concept</description>
		<content:encoded><![CDATA[<p>Such a helpful link for javascript inheritance concept</p>]]></content:encoded>
	</item>
	<item>
		<title>By: M</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/comment-page-1/#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&#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 :).</p>]]></content:encoded>
	</item>
	<item>
		<title>By: abigdog</title>
		<link>http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/comment-page-1/#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]) == &#039;function&#039;) {
            descendant.prototype[aMatch[1]+&#039;_&#039;+m] = parent.prototype[m];
        }
        descendant.prototype[m] = parent.prototype[m];  
    }  
}

my humble addition allows parent methods to be called without using &quot;apply&quot;.

if B extends A as in Mr. Fuecks examples, and &quot;obj&quot; is an instance of A, and &quot;category&quot; a virtual function, class A&#039;s version can be invoked on obj as &quot;obj.A_category()&quot; similar to the c++ syntax &quot;obj-&gt;A::category()&quot;</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-page-1/#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&#039;s simple and good enough for my needs. 
As said, I&#039;m happy that it doesn&#039;t need copyPrototype and is possible to call the base class method which has been overridden.

  &lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;function Animal(type) 
      { 
            alert(&#039;Base class constructor&#039;); 

	    this.species = type; 
	    
	     //this method doesn’t need to be called from the derived class!
             this.SayHello = SayHello;
             
             function SayHello()
             {
                 alert (&quot;Base class SayHello() &#039;&quot;+this.species+&quot;&#039;&quot;);
             } 
	
	     
	     //must be overridden  in the derived class
     	     Animal.prototype.Category = function()
	     { 
	            alert(&#039;Base class Category() &#039; +this.species); 
	     }
   	 
 
	}
	
         
	  
		 
	function Dog(type) 
	{ 
            alert(&#039;Derived class constructor&#039;); 

	    // 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(&quot;Derived class Category() &quot; +this.species); 
	    }
     
    }
   &lt;/code&gt;&lt;/pre&gt;

   &lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;var d = new Dog(&#039;dog&#039;); 
   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-page-1/#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&#039;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=&quot;javascript&quot;&gt;    function BaseClass(divID) 
    { 
               
        alert(&quot;  Base class constructor&quot;); 
        this.divID=divID;
       
        
        //this method doesn&#039;t need to be called from the derived class!
        this.SayHello = SayHello;
        
        function SayHello()
        {
              alert (&quot;Base class SayHello() &#039;&quot;+this.divID+&quot;&#039;&quot;);
        } 
   
  
     //this method must be overridden in the derived class
      BaseClass.prototype.SayGoodbye = function(arg) 
      { 
            alert(&quot;Base class SayGoodbye(): &#039;&quot;+this.divID+&quot;&#039; &quot;+arg); 
      }                 
        
	          
    } 
   
   
   
    function DerivedClass(divID) 
    { 
   
        alert(&quot;  Derived class constructor&quot;); 
        
       //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(&quot;Derived class SayGoodbye(): &#039;&quot;+this.divID+&quot;&#039; &quot;+arg); 
        } 
           
    } 

   var b = new BaseClass(&quot;base class&quot;); 
   b.SayGoodbye(102);     
   b.SayHello(); 

    var d = new DerivedClass(&quot;derived class&quot;); 
    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-page-1/#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&#039;ve &lt;a href=&quot;http://www.lshift.net/blog/2006/08/03/subclassing-in-javascript-part-2&quot; rel=&quot;nofollow&quot;&gt;blogged about an alternate approach which seems considerably more elegant to me &lt;/a&gt;- I&#039;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-page-1/#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=&#039;javascript&#039;&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&#039;s constructor will also match the space after (and before) the constructor&#039;s name.  Hence instead of grabbing &quot;Animal&quot; only, the pattern will also grab &quot;Animal &quot; 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&#039;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=&#039;javascript&#039;&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,&quot;&quot;)] = 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,&quot;&quot;)&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-page-1/#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>[...] I want to achieve the above without resorting to global functions to build prototype chains [...]</p>]]></content:encoded>
	</item>
</channel>
</rss>
