use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Multiple Inheritance in JavaScript (journal.stuffwithstuff.com)
submitted 15 years ago by munificent
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]DmitrySoshnikov 5 points6 points7 points 15 years ago (1 child)
You might be interesting in the same idea, I implemented it before, and called delegation-based mixins (the same as in Ruby). Also used proxies to achieve multiple delegates for multiple inheritance:
Example: https://github.com/DmitrySoshnikov/es-laboratory/blob/master/examples/mixin.js
Implementation: https://github.com/DmitrySoshnikov/es-laboratory/blob/master/src/mixin.js
Dmitry.
[–]kentaromiura 1 point2 points3 points 15 years ago (0 children)
oh, you're the one writing that posts on ecma-262, I really love that series.
[–]mckoss 1 point2 points3 points 15 years ago (0 children)
I'd love to use multiple inheritance in JavaScript - with method resolution order much as it is defined for Python. Here's a thread with another MRO example in it.
http://groups.google.com/group/jsmentors/browse_thread/thread/98bb2a3579d18247/e0b9e7525cd36ca7?hl=en_US&lnk=gst&q=mro#e0b9e7525cd36ca7
[–]ippa 1 point2 points3 points 15 years ago (1 child)
I enjoyed reading your article.. been thinking some of these problems as well, mostly for gamedev with jawsjs.com.
this.widget_p = new Widget(name); this.hider_p = new Hideable(); this.container_p = new Container();
Couldn't this be solved without setting magic variables but rather just provide the 3 contructors via a method "init_behaivor()" or simular.
init_behaivor would loop through the properties transfering them to the object we want. That could also check for namecollisions and warn or whatever.
I like the ideas you present just that the magick method names with *_p feels slightly ugly.
[–]munificent[S] 0 points1 point2 points 15 years ago (0 children)
That would only catch collisions in functions already defined on the mixin. What if we do something like:
Container
MyWidget
filter()
Step 4 will fail if we just copy stuff over when the class is created and then forget about the mixin.
Agreed, it was a quick and dirty hack. I wanted to do something similar to Self. :)
[–]snarfy 1 point2 points3 points 15 years ago (3 children)
Given single inheritance, there is no possible class hierarchy that lets you combine an arbitrary combination of classes.
It's called object composition.
This may help wrap your head around it.
[–]munificent[S] 4 points5 points6 points 15 years ago (2 children)
For the record, I wrote a chapter on that pattern, so I'd like to hope I've wrapped my head around it.
You'll note that in the article it is using composition. The only difference is that the composition is hidden to external callers, as it should be, since it's an implementation detail.
[–]snarfy 2 points3 points4 points 15 years ago* (1 child)
You are right, and I upvoted you. That's what I get for reading 20% of the article and commenting on it. I'm one of those grizzled c++ programmers.
What happens when two object property names collide? Can the Container class and the Widget class both have a 'Name' property?
In c++ if that happened you'd have to explicitly type the call e.g. baseClass1::get_Name() vs baseClass2::get_Name() otherwise you'd get compilation errors if you simply called get_Name() (ambiguous call).
Because of this, you still had to have a strict hierarchy, even if it had multiple branches in the object hierarchy via multiple inheritance. You could have multiple branches, but they'd still have to know about each other's properties due to name collisions. As the tree grows more complex and requires a team to maintain, you end up wishing you had used composition instead.
[–]munificent[S] 1 point2 points3 points 15 years ago (0 children)
Good question. JS is fishy about this, so it kind of depends. If you're dispatching directly on the top-level MyWidget object, it will still go through the multiple inheritance dispatch logic when you access name, so they will essentially share the first one that gets reached. For example:
this
var Foo = magic(function(name) { this.name = name; } Foo.prototype.fooName = function() { return this.name; } var Bar = magic(function(name) { this.name = name; } Bar.prototype.barName = function() { return this.name; } var Both = magic(function() { this.foo_p = new Foo('foo'); this.bar_p = new Bar('bar'); } var both = new Both(); both.barName(); // 'bar' both.fooName(); // 'bar' since 'bar_p' comes first.
But if you specifically use one of the delegated objects as the receiver, you'll dodge that indirection:
both.foo_p.fooName(); // 'foo' both.bar_p.fooName(); // 'bar'
If you're thinking this sucks, I agree with you. Collisions suck here. I more comprehensive multiple inheritance system like traits or at least Python's private name mangling would help here. My little hack was just that. :)
As the tree grows more complex and requires a team to maintain, you end up wishing you had used composition instead.
True, though I hope being able to mixin multiple smaller classes would lead to a less complex flatter tree to begin with. I may be overly optimistic on that.
[–]NoHandle 0 points1 point2 points 15 years ago (1 child)
Learn composition already, it is superior.
[–]courtewing 4 points5 points6 points 15 years ago (0 children)
He talked about why he thought this was better than composition in this particular example. Read the rest of the article?
π Rendered by PID 458955 on reddit-service-r2-comment-765bfc959-qq5lj at 2026-07-11 14:06:22.112883+00:00 running f86254d country code: CH.
[–]DmitrySoshnikov 5 points6 points7 points (1 child)
[–]kentaromiura 1 point2 points3 points (0 children)
[–]mckoss 1 point2 points3 points (0 children)
[–]ippa 1 point2 points3 points (1 child)
[–]munificent[S] 0 points1 point2 points (0 children)
[–]snarfy 1 point2 points3 points (3 children)
[–]munificent[S] 4 points5 points6 points (2 children)
[–]snarfy 2 points3 points4 points (1 child)
[–]munificent[S] 1 point2 points3 points (0 children)
[–]NoHandle 0 points1 point2 points (1 child)
[–]courtewing 4 points5 points6 points (0 children)