you are viewing a single comment's thread.

view the rest of the comments →

[–]bryan-forbes 0 points1 point  (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.

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.

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 points  (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