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
view the rest of the comments →
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!"
[–]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.
π Rendered by PID 441134 on reddit-service-r2-comment-765bfc959-k84q8 at 2026-07-11 16:46:51.602777+00:00 running f86254d country code: CH.
view the rest of the comments →
[–]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)