Evandro M. contacted me about my earlier post on MRO implementations
for JavaScript. I think I've come up with a nice approach today that
works with a minimum of excess baggage (i.e., uses native prototype-
based inheritance in as much as possible).

Example of diamond-shape multiple inheritance:

http://wiki.pageforest.com/#js-patterns/mro

In this implementation, I require that the developer provide the
linearization order of the classes they want to inherit from.  A
further extension to this would compute the ordering based on the C3
linearization algorithm.  But, in my experience, it's pretty easy to
directly specify the order desired for calling super classes.

I've added this to a new library I've been building up with basic
modularization functions and object-oriented helpers, called
"namespace".

http://github.com/mckoss/namespace

Docs describing the MRO function:

https://github.com/mckoss/namespace/wiki/funcs.js

MRO Source:

function mro(ctors, extraMethods) {
    var parent = ctors.pop().prototype;
    var ctor;
    while (ctors.length > 0) {
        ctor = ctors.pop();
        var ctorName = types.getFunctionName(ctor);
        var proto = exports.create(parent);
        types.extend(proto, ctor.prototype);
        proto.constructor = ctor;
        proto[ctorName + '_super'] = parent;
        parent = proto;
    }
    ctor.prototype = parent;
    methods(ctor, extraMethods);
}

Unit Test Runner:

http://namespace-js.pageforest.com/test/test-runner.html

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to