本文介绍了使用MooTools类的静态方法和变量的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何最佳实践或常见解决方案为MooTools生成的类添加对静态方法和变量的支持?

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?

推荐答案

Class 系统(MooTools要么是灵感来自还是原型的分支,取决于你问的是谁) 。

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).

这篇关于使用MooTools类的静态方法和变量的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 19:57