devios1 devios1 - 1 year ago 78
Javascript Question

Multiple inheritance/prototypes in JavaScript

I've come to a point where I need to have some sort of rudimentary multiple inheritance happening in JavaScript. I'm not here to discuss whether this is a good idea or not, so please keep those comments to yourself.

I just want to know if anyone's attempted this with any (or not) success, and how they went about it.

To boil it down, what I really need is to be able to have an object capable of inheriting a property from more than one prototype chain (i.e. each prototype could have its own proper chain), but in a given order of precedence (it will search the chains in order for the first definition).

To demonstrate how this is theoretically possible, it could be achieved by attaching the secondary chain onto the end of the primary chain, but this would affect all instances of any of those previous prototypes and that's not what I want.


Appreciate the responses folks, but while the consensus seems to be statically copying over the properties from both trees, which would work in most cases (and will probably be what I end up doing), I was most interested in a dynamic solution that would allow the separate prototype chains to be altered, and still have those changes "picked up" by the instance.

Answer Source

Mixins can be used in javascript to achieve the same goal you probably want to solve via multiple inheritance at the moment.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download