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

8

有没有关于在MooTools生成的类中添加对“静态”方法和变量支持的最佳实践或常见解决方案?

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

4个回答

5

注意:我从未使用过MooTools。虽然我用过类似的Class系统Prototype(根据不同的人说法,MooTools是“受其启发”还是“分支”于Prototype)。

只需将它们作为“class”的属性添加即可:

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

以上第一行是来自文档;其余为我添加的内容。

在任何新实例的initialize之前,你都知道会发生这种情况,因为你没有留下创建新实例的机会,以便附加静态方法(或属性)。


staticMethod将不可用于MyClass的实例,不确定是否需要此功能,但请记住。您可以调用MyClass.staticMethod() - 为了增加可读性,请使用MyClass.extend({ method: function() ... , method2: function() ...}); - Dimitar Christoff
3
staticMethod方法对于MyClass的实例来说也是可用的,可以通过MyClass.staticMethod()进行调用,就像其他任何东西一样。 - T.J. Crowder
这正是问题所在,根据我的经验,它不可用 - http://www.jsfiddle.net/fWQBM/ -> 你可以在 MyClass 上调用它,但不能在实例上调用。.extend 也是如此 - 但你可以直接向实例添加方法。mootools !=== prototype - Dimitar Christoff
3
@Dimitar:没错,原帖是在问如何添加静态方法,而这份代码就是为此而存在。如果你需要实例方法,可以在传递给new Class的属性中定义它们。 - T.J. Crowder
3
我知道,只是想提一下,因为当我第一次尝试向另一个地方定义的类添加新方法时,这让我感到困惑 :) - Dimitar Christoff

4
我知道这篇文章有点老了,但我想给出比已有答案更好的回答。
我建议使用以下语法来编写静态方法:
var MyClass = new Class({
    initialize: function() {
        this.method();
        MyClass.staticMethod();
    }
    ,
    method: function() {}
}).extend({
    staticMethod: function() {}
});

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

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


1

开胃菜...

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

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

  1. 属性

    MyObject.staticMethod = new function() {};
    
    MyObject.staticMethod(); // 调用静态方法。
    
  2. 方法

    MyObject.prototype.instanceMethod = new function() {};
    
    new MyObject().instanceMethod(); // 调用实例方法。
    

主菜单...

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

我包含了一些Arcabard答案缺少的信息。

1. 作为对象属性

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);
};

使用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. 向 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'

-1

来自《使用Mootools的专业JavaScript》的摘录:

通过Function.prototype声明了extend方法,并被所有函数继承。因为MooTools类本质上是一个函数,我们也可以将extend方法用于类。该方法与implement方法类似,都接受一个对象参数,其中键和值引用将要添加的成员。不同的是,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()以获取更多信息。


2
你在这里使用的 "Static:" mutator 不是 MooTools 的一部分 -- 它实际上是本书早期的示例代码。此外,这个例子甚至没有展示如何使用 .extend() - Scott Rippey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接