开发者

Inheritance of static members in MooTools

I'm using the MooTools class system, and I'd like to be able to access any static member of a particular class without having to know the full inheritance chain. For example, if I have a ChildClass that extends BaseClass, and

BaseClass.foo = function() { /*...*/ }

I'd like to be able to call ChildClass.foo().

To this end, I'm thinking of modifying the MooTools Class method as follows:

function Class(params)
    // ...
    // var newClass = ...

    var parentClass = param开发者_JAVA百科s.Extends;
    if (parentClass) {
        newClass.__proto__ = parentClass;
    }

    // ...
}

This will set up each class object's prototype chain to point to its parent class.

If a static member from a higher class is hidden in a more derived class, so be it.

Notwithstanding the use of the deprecated __proto__, am I on the right track here? Does anyone see any glaring problems?


You can always extend the Extend Mutator:

(function(){

    var original = Class.Mutators.Extends;

    Class.Mutators.Extends = function(parent) {
        original.call(this, parent);

        var members = {};
        for (var key in parent) {
            if (parent.hasOwnProperty(key) && !this.hasOwnProperty(key)) {
                members[key] = parent[key];
            }
        }
        this.extend(members);
    };

}());

You should check out Mark Obcena's book.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜