Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For the below code, that creates inheritance hierarchy,

function Animal(){
    this.name = "Animal";

    // toString is a function in the main Object that every
    // object inherits from
    this.toString = function() {
        return "My name is : " + this.name;
    };
}


function Rodent(){
   this.name = "Rodent";
}

function Rat(){
  this.name = "Rat";
}

Rodent.prototype = new Animal();
Rat.prototype = new Rodent();

Rodent.prototype.constructor = Rodent;
Rat.prototype.constructor = Rat;

var caneRat = new Rat();

document.write(caneRat.toString() + "<br />");

a little modification is done by completely replacing Rat.prototype with Rodent.prototype, as a second approach, shown below,

function Animal(){
    this.name = "Animal";

    // toString is a function in the main Object that every
    // object inherits from
    this.toString = function() {
        return "My name is : " + this.name;
    };
}


function Rodent(){
   this.name = "Rodent";
}

function Rat(){
  this.name = "Rat";
}

Rodent.prototype = new Animal();
Rat.prototype = Rodent.prototype;
Rodent.prototype.constructor = Rodent;

var caneRat = new Rat();

document.write(caneRat.toString() + "<br />");  

What are the advantages & disadvantages in the second approach of inheritance hierarchy design compared to first approach? Second approach is visualized as shown below..

enter image description here

share|improve this question

closed as too broad by Scant Roger, durron597, GlenH7 Dec 8 '15 at 17:18

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

    
@Bergi Is this the wrong approach in designing inheritance hierarchy? – overexchange Dec 8 '15 at 3:57
    
Yes, it's wrong. Rat and Rodent don't inherit from each other. – Bergi Dec 8 '15 at 4:02
    
@Bergi How do I recorrect this code? – overexchange Dec 8 '15 at 4:28
    
Your first attempt seemed better, although you should not use new at all for creating prototype objects. – Bergi Dec 8 '15 at 4:53
    
@Bergi Can you please share the code without a new keyword for prototype objects for first approach? – overexchange Dec 8 '15 at 5:05

It's generally fairly awkward and error-prone to implement "classical inheritance" in JavaScript. Instead, try working with the language and use behavior delegation:

var Animal = {
  setName: function(name) {
    this.name = name;
  },

  sayName: function() {
    return 'My name is: ' + this.name;
  }
};

var Rodent = Object.create(Animal);
var Rat = Object.create(Rodent);
var caneRat = Object.create(Rat);

caneRat.setName('Rat');

console.log(caneRat.sayName()); // 'My name is Rat'

You'll probably find that you can eliminate a lot of your "subclasses" with this approach.

For more detail on prototypes in JavaScript, take a look at this.

Also, try to avoid overwriting native object methods (e.g. toString) unless you have a really good reason to do so.

share|improve this answer
    
In this hierarchy all are function constructors – overexchange Dec 30 '15 at 6:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.