1

What are the ways of achieving inheritance in javascript. I am getting confused on the OOPS as i see different ways of doing the same thing.

Below is a simple parent/child relationship.

function Plant () {
​   this.country = "Mexico";
​   this.isOrganic = true;
}
​
​Plant.prototype.showNameAndColor =  function () {
    console.log("I am a " + this.name + " and my color is " + this.color);
}
​
​Plant.prototype.amIOrganic = function () {
    ​if (this.isOrganic)
    console.log("I am organic, Baby!");
}
​
​function Fruit (fruitName, fruitColor) {
​    this.name = fruitName;
​    this.color = fruitColor;
}
​
​Fruit.prototype = new Plant ();
​var aBanana = new Fruit ("Banana", "Yellow");
​console.log(aBanana.name); // Banana​
​console.log(aBanana.showNameAndColor()); // I am a Banana and my color is yellow.

First Question:

I have seen in some codes there are setting the constructor of the children too, this makes me quite confusing?. Why do people also set Fruit.prototype.constructor = Plant;

Is the below code not enough to achieve inheritance Fruit.prototype = new Plant(); // Fruit has all methods/properties of Plant Object

Second Question:

Passing values to Super constructor.

Fruit.prototype = new Plant ("Aus", true);

OR

function Fruit (fruitName, fruitColor) {
    Plant.call(this,"Aus", true);
​    this.name = fruitName;
​    this.color = fruitColor;
}

Why is the former always preferred than the later while passing arguments to parent constructor?.

Shane
  • 5,517
  • 15
  • 49
  • 79
  • The former is completely wrong. http://blog.slaks.net/2013-09-03/traditional-inheritance-in-javascript/ – SLaks Dec 09 '14 at 22:26
  • 2
    *"Why is the former always preferred than the later while passing arguments to parent constructor?."* It's not... at all. See [Benefits of using `Object.create` for inheritance](http://stackoverflow.com/a/17393153/218196). And if people set `Fruit.prototype.constructor = Plant;` then that's just wrong. It should be `Fruit.prototype.constructor = Fruit;`. I recommend to have a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance . Also, there have been tons of questions about inheritance here on SO. – Felix Kling Dec 09 '14 at 22:35
  • @FelixKling: Thanks a lot for your linked answer, can you explain me on why this " But it also implies that every dog inherits from one specific Animal instance. That seems to be a bit strange. Shouldn't instance specific code only be run in the constructor function? Suddenly instance specific code and prototype methods seem to be mixed." I did not quite understand on why Fruit.prototype = new Plant() is bad approach? Thanks a lot again... – Shane Dec 09 '14 at 22:43
  • @SLaks: I am reading your blog post, thanks for the link. – Shane Dec 09 '14 at 22:43
  • 2
    It's bad because you are creating an instance of something when you should not. What if `Plant` *requires* an argument to be set? It will error. You could pass an arbitrary value, like `Fruit.prototype = new Plant('aqewtqweg')` to make the code work. But that alone should convince you that this is not the right approach. Another example: `SmallDog.prototype = new Dog('Paul');`. Is *every* `SmallDog` really a "child" of a dog named Paul? Who is this Paul, and what does he have to do with `SmallDog`? – Felix Kling Dec 09 '14 at 22:47
  • @FelixKling—I think that's a [straw man](http://en.wikipedia.org/wiki/Straw_man) argument. Constructors should only require arguments that apply to all their instances, including where used as super classes. – RobG Dec 09 '14 at 23:05

0 Answers0