Possible Duplicate:
Javascript multiple inheritance
Is there a way in JavaScript to do this:
Foo = function() {
};
Bar = function() {
};
Baz = function() {
Foo.call(this);
Bar.call(this);
};
Baz.prototype = Object.create(Foo.prototype, Bar.prototype);
var b = new Baz();
console.log(b);
console.log(b instanceof Foo);
console.log(b instanceof Bar);
console.log(b instanceof Baz);
So that Baz is both an instance of Foo and Bar?
JavaScript does not have multiple inheritance. instanceof
tests the chain of prototypes, which is linear. You can have mixins, though, which is basically what you're doing with Foo.call(this); Bar.call(this)
. But it is not inheritance; in Object.create
, the second parameter only gives properties to copy, and is not a parent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With