有没有关于在MooTools生成的类中添加对“静态”方法和变量支持的最佳实践或常见解决方案?
特别是,是否有任何解决方案可以确保在调用实例initialize
方法之前进行静态初始化?
有没有关于在MooTools生成的类中添加对“静态”方法和变量支持的最佳实践或常见解决方案?
特别是,是否有任何解决方案可以确保在调用实例initialize
方法之前进行静态初始化?
注意:我从未使用过MooTools。虽然我用过类似的Class
系统Prototype(根据不同的人说法,MooTools是“受其启发”还是“分支”于Prototype)。
只需将它们作为“class”的属性添加即可:
var MyClass = new Class(properties);
MyClass.staticMethod = function() {
// ...
};
以上第一行是来自文档;其余为我添加的内容。
在任何新实例的initialize
之前,你都知道会发生这种情况,因为你没有留下创建新实例的机会,以便附加静态方法(或属性)。
var MyClass = new Class({
initialize: function() {
this.method();
MyClass.staticMethod();
}
,
method: function() {}
}).extend({
staticMethod: function() {}
});
.extend({})
方法是在一个类上添加静态方法的标准方法。
唯一让我不太喜欢的是 MyClass.staticMethod();
的语法,但其实没有更好的选择了。
JavaScript中的静态方法是引用它们的对象的属性。它们不会添加到对象的原型中。
在JavaScript中,有两种将函数添加到对象的方法。下面,我将方法添加到一个名为"MyObject
"的虚构对象中。
属性
MyObject.staticMethod = new function() {};
MyObject.staticMethod(); // 调用静态方法。
方法
MyObject.prototype.instanceMethod = new function() {};
new MyObject().instanceMethod(); // 调用实例方法。
有三种方法可以将静态方法添加到类中。下面的代码源自Mark Obcena的“Pro JavaScript with MooTools”。
我包含了一些Arcabard答案缺少的信息。
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);
}
});
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'
来自《使用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()
以获取更多信息。
.extend()
。 - Scott Rippey
MyClass.staticMethod()
- 为了增加可读性,请使用MyClass.extend({ method: function() ... , method2: function() ...});
- Dimitar ChristoffstaticMethod
方法对于MyClass
的实例来说也是可用的,可以通过MyClass.staticMethod()
进行调用,就像其他任何东西一样。 - T.J. Crowdernew Class
的属性中定义它们。 - T.J. Crowder