JavaScript multiple inheritance and instance

Possible duplicate:
Javascript multiple inheritance

Is there a JavaScript way 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, is Baz an instance of Foo and Bar?

+6
source share
1 answer

JavaScript does not have multiple inheritance. instanceof checks the prototype chain which is linear. You may have mixins, although this is basically what you do with Foo.call(this); Bar.call(this) Foo.call(this); Bar.call(this) . But this is not inheritance; in Object.create , the second parameter gives the properties to copy and is not a parent.

+7
source

All Articles