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.