使用MooTools类的静态方法和变量的最佳实践

时间:2010-11-29 16:07:26

标签: javascript oop static mootools

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

特别是,是否有任何解决方案可以确保在调用实例initialize方法之前进行静态初始化?

4 个答案:

答案 0 :(得分:5)

警告:从未使用过MooTools。我曾经使用过Prototype,它有一个类似的Class系统(MooTools的灵感来自于“或者原型的分叉,取决于你问的是谁)。

只需将它们作为属性添加到生成的“类”中:

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

(上面的第一行来自the docs;剩下的就是我的补充。)

您知道在initialize之前会在任何新实例上发生,因为在附加静态方法(或属性)之前,您没有留下创建新实例的机会。

答案 1 :(得分:4)

我知道这篇文章已经过时了,但我想提供一个比已经说过的更好的答案 我推荐静态方法的以下语法:

var MyClass = new Class({
    initialize: function() {
        this.method();
        MyClass.staticMethod();
    }
    ,
    method: function() {}
}).extend({
    staticMethod: function() {}
});

.extend({})方法是在Class上添加静态方法的标准方法。

我唯一不喜欢的是MyClass.staticMethod();语法,但没有更多更好的选择。

答案 2 :(得分:1)

Appitizer ...

JavaScript中的静态方法是引用它们的Object的属性。它们不会添加到Object的原型中。

有两种方法可以在JavaScript中向对象添加函数。下面,我将方法添加到一个名为" MyObject"的虚构对象中。

  1. 属性

    MyObject.staticMethod = new function() {};
    
    MyObject.staticMethod(); // Call static method.
    
  2. 方法

    MyObject.prototype.instanceMethod = new function() {};
    
    new MyObject().instanceMethod(); // Call instance method.
    
  3. 主要课程......

    有三种方法可以将静态方法添加到类中。下面的代码源自Mark Obcena的" Pro JavaScript with MooTools"

    我收录了James Andino的答案中缺少的更多信息。

    1。作为Object属性

    var Person = new Class({
        // Instance Variables
        name: '',
        age: 0,
        // Constructor
        initialize: function(name, age) {
            this.name = name;
            this.age = age;
        },
        // Instance Methods
        log: function() {
            console.log(this.name + ', ' + this.age);
        }
    });
    
    // Static Property
    Person.count: 0;
    // Static Methods
    Person.addPerson: function() {
        this.count += 1;
    };
    Person.getCount: function() {
        console.log('Person count : ' + this.count);
    };
    

    2。使用extend()

    var Person = new Class({
        // Instance Variables
        name: '',
        age: 0,
        // Constructor
        initialize: function(name, age) {
            this.name = name;
            this.age = age;
        },
        // Instance Methods
        log: function() {
            console.log(this.name + ', ' + this.age);
        }
    });
    
    Person.extend({
        // Static Property
        count: 0,
        // Static Methods
        addPerson: function() {
            this.count += 1;
        },
        getCount: function() {
            console.log('Person count : ' + this.count);
        }
    });
    

    3。将新的mutator添加到Class.Mutators

    // This will create a shortcut for `extend()`.
    Class.Mutators.Static = function(members) {
        this.extend(members);
    };
    
    var Person = new Class({
        Static: {
            // Static Property
            count: 0,
            // Static Method
            addPerson: function() {
                this.count += 1;
            },
            getCount: function() {
                console.log('Person count : ' + this.count);
            }
        },
        // Instance Variables
        name: '',
        age: 0,
        // Constructor
        initialize: function(name, age) {
            this.name = name;
            this.age = age;
        },
        // Instance Methods
        log: function() {
            console.log(this.name + ', ' + this.age);
        }
    });
    

    使用静态方法的示例。

    // Creating a new Person instance
    var mark = new Person('Mark', 23);
    mark.log();
    
    // Accessing the static method
    Person.addPerson();
    Person.getCount() // 'Person count: 1'
    

答案 3 :(得分:-1)

摘自“Pro Javascript with Mootools”

  

extend方法通过Function.prototype声明,并由所有函数继承。因为MooTools类本质上是一个函数,所以我们也可以对类使用extend方法。该方法类似于实现方法,因为它使用带有键的对象参数和引用将添加的成员的值。但是,与实现不同,extend不会将成员添加到类的原型中,而是添加到类对象本身。

// You can implement a `Static` mutator for creating static methods and variables:
Class.Mutators.Static = function(members) {
    this.extend(members);
}

// Using the Static mutator above
var Person = new Class({
    Static: {
        // Static Property
        count: 0,
        // Static Method
        addPerson: function() {
            this.count += 1;
        },
        getCount: function(){
            console.log('Person count: ' + this.count);
        }
    }
});

有关详细信息,请参阅MooTools: Types > Function > extend()