1

How can an object inherit from multiple parents in javascript?

I am thinking one would do this:

Fruit.prototype = new Plant ();
Fruit.prototype = new anotherPlant ();

But what will the Fruit's prototype attribute (prototype object) get set to? Will it still be the original Parent.prototype of the original Parent constructor function of Fruit?

Tomatoes
  • 91
  • 1
  • 7
  • Check this out http://stackoverflow.com/questions/9163341/multiple-inheritance-prototypes-in-javascript – PSL Nov 25 '13 at 23:12

1 Answers1

1

You can't. Infact, not many languages support multiple inheritance.

All you are doing there is setting the prototype of Fruit to an instance of Plant, and then overwriting it with an instance of anotherPlant. It's the same as simply;

Fruit.prototype = new anotherPlant ();

However, don't forget that JavaScript has an inheritance chain. Using the above, if anotherPlant had Plant as it's prototype, you'd inherit from both objects.

function Plant() {

}

Plant.prototype.foo = 'foo';
Plant.prototype.baz = 'baz-a';

function AnotherPlant() {

}

AnotherPlant.prototype = new Plant();

AnotherPlant.prototype.bar = 'bar';
AnotherPlant.prototype.baz = 'baz-b';

var a = new AnotherPlant();

console.log(a.foo); // foo
console.log(a.bar); // bar
console.log(a.baz); // baz-b

JavaScript does inheritance differently to most other languages; it uses prototypical inheritance, which means the language determines the inheritance chain of a function (a class, if that makes it easier) by following the prototype property of the constructor function used to create the instance.

Matt
  • 74,352
  • 26
  • 153
  • 180
  • hmmm.... Matt also if I do Fruit.prototype = new Plant (); I am setting the prototype attribute (not the prototype property) to point to Plant.prototype So does a prototype has both characteristics ie its used to define prototypal properties for children to inherit and also a prototypal attribute to point to a parent. ie its the same thing doing two things? – Tomatoes Nov 25 '13 at 23:16
  • @Tomatoes: If you do that, you'd inherit the `foo` and `baz` properties of `Plant`. What are you expecting to happen? – Matt Nov 25 '13 at 23:17
  • srry i wasnt clear, I am trying to distinguish between the prototype property and the prototype attribute and trying to figure out what the Fruit.prototype = new Plant (); statement it affecting. Seems like this one statement may well be doing two things. Inheritance = inheriting Plant.prototype methods AND assigning Plant.prototype as its prototype object (parent pointer). – Tomatoes Nov 25 '13 at 23:22
  • @Tomatoes: A property and an attribute are the same thing. `Fruit.prototype = new Plant()` is setting the prototype property (or attribute) of `Fruit` to a new instance of `Plant`. JavaScript's inheritance is prototypical, rather the class-based, which may be what is confusing you; see http://en.wikipedia.org/wiki/Prototype-based_programming – Matt Nov 25 '13 at 23:25
  • how can it be though? one is defining properties for its child instances to inherit and the other is a pointer to its own parent. srry I am coming from another language. yeah checked all the tuts and everything. still dont know how one thing can do two different things. – Tomatoes Nov 25 '13 at 23:26
  • that wiki page doesnt answer my question but appreciate the help. – Tomatoes Nov 25 '13 at 23:29
  • also, i understand prototypal inheritance and how it follows the chain I just cant distinguish between the behavior i mentioned above. – Tomatoes Nov 25 '13 at 23:32
  • i got it now. Prototype attribute is just a characteristic which tells us who the parent is so we can go up the chain. Prototype property lets us set methods and properties which children can inherit. Prototype does both because prototype attribute is just a characteristic. – Tomatoes Nov 26 '13 at 00:12