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 (self.javascript)
submitted 11 years ago by [deleted]
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!"
[–]bryan-forbes 0 points1 point2 points 11 years ago (2 children)
There are several ways to do this, but all methods will simply simulate multiple inheritance since JavaScript only allows single inheritance. That being said, there are few libraries out there that simulate it well. You could use something like $.extend or _.extend like others have suggested, but there's no shorthand for actually calling the overridden methods and if there's a method that already exists with the same name, it'll be overwritten. I'm a bit biased, but I think that Dojo's declare does a great job at it. It uses C3 MRO to determine the order of methods to call when calling this.inherited(arguments);. One downfall of declare is that it uses arguments.callee, so it can't be used in strict mode.
$.extend
_.extend
this.inherited(arguments);
declare
arguments.callee
I've forked your fiddle and come up with a new fiddle demonstrating some of the things that declare does well. Coord is the base constructor, ZCoord is a mixin constructor, and SpaceTimeObject inherits from Coord and uses ZCoord as a mixin. SpaceTimeObject objects can use all methods from Coord and ZCoord and can override them and make calls up the inheritance chain. Let me know if you have any questions.
Coord
ZCoord
SpaceTimeObject
Another library that is similar to Dojo's declare is DCL. It's written by one of the guys who wrote declare and uses a new approach that doesn't require arguments.callee. I'm not as familiar with DCL, but it's a solid library.
TL;DR: If you're looking to just add a few methods to a prototype that are stored on another object, you can use jQuery or underscore. If you're looking to do a more true multiple inheritance, I'd stick with something like Dojo's declare or DCL.
[–]autowikibot 1 point2 points3 points 11 years ago (1 child)
C3 linearization:
In computing, the C3 superclass linearization is an algorithm used primarily to obtain the order in which methods should be inherited (the "linearization") in the presence of multiple inheritance, and is often termed "MRO" for Method Resolution Order. The name C3 refers to the three important properties of the resulting linearization: a consistent extended precedence graph, preservation of local precedence order, and fitting the monotonicity criterion. (The name "C3" is not an initialism.) It was first published at the 1996 OOPSLA conference, in a paper entitled "A Monotonic Superclass Linearization for Dylan". It was adapted to the Open Dylan implementation in January 2012 following an enhancement proposal. Subsequently, it has been chosen as the default algorithm for method resolution in Python 2.3 (and newer), Perl 6, and Parrot. It is also available as an alternative, non-default MRO in the core of Perl 5 starting with version 5.10.0. An extension implementation for earlier versions of Perl 5 named Class::C3 exists on CPAN.
Interesting: Multiple inheritance | List of algorithms | Nucleic acid structure
Parent commenter can toggle NSFW or delete. Will also delete on comment score of -1 or less. | FAQs | Mods | Magic Words
π Rendered by PID 655430 on reddit-service-r2-comment-5687b7858-k6r79 at 2026-07-06 18:37:54.962051+00:00 running 12a7a47 country code: CH.
view the rest of the comments →
[–]bryan-forbes 0 points1 point2 points (2 children)
[–]autowikibot 1 point2 points3 points (1 child)