Search code examples
javascriptoopstaticmootools

Best practices for static methods and variables with MooTools classes


Are there any best practices or common solutions to adding support for "static" methods and variables to MooTools-generated classes?

In particular, is there any solution that ensures that static initialization takes place before the instance initialize method is called?


Solution

  • Caveat: Never used MooTools. I've used Prototype a fair bit, though, which has a similar Class system (MooTools is either "inspired by" or a fork of Prototype, depending on who you ask).

    Just add them as properties on the resulting "class":

    var MyClass = new Class(properties);
    MyClass.staticMethod = function() {
        // ...
    };
    

    (The first line above is from the docs; the remainder is my addition.)

    You know that will happen prior to initialize on any new instance because you're not leaving an opportunity for creating a new instance prior to attaching your static methods (or properties).