【发布时间】:2011-05-17 09:00:56
【问题描述】:
是否有任何最佳实践或通用解决方案可以向 MooTools 生成的类添加对“静态”方法和变量的支持?
具体来说,是否有任何解决方案可以确保在调用实例initialize 方法之前进行静态初始化?
【问题讨论】:
标签: javascript oop static mootools
是否有任何最佳实践或通用解决方案可以向 MooTools 生成的类添加对“静态”方法和变量的支持?
具体来说,是否有任何解决方案可以确保在调用实例initialize 方法之前进行静态初始化?
【问题讨论】:
标签: javascript oop static mootools
警告:从未使用过 MooTools。不过,我用过 Prototype,它有一个类似的 Class 系统(MooTools 要么是“受启发”,要么是 Prototype 的一个分支,这取决于你问谁)。
只需将它们作为属性添加到生成的“类”上:
var MyClass = new Class(properties);
MyClass.staticMethod = function() {
// ...
};
(上面第一行来自the docs;其余是我的补充。)
您知道这将在 initialize 之前在任何新实例上发生,因为您不会在附加静态方法(或属性)之前留下创建新实例的机会。
【讨论】:
MyClass.staticMethod() - 为了提高可读性,请使用MyClass.extend({ method: function() ... , method2: function() ...});
staticMethod 将可用于MyClass 的实例,就像其他任何东西一样,如MyClass.staticMethod()。
new Class的属性中定义它们。
我知道这篇文章已经过时了,但我想给出一个比已经说明的更好的答案。
我建议静态方法使用以下语法:
var MyClass = new Class({
initialize: function() {
this.method();
MyClass.staticMethod();
}
,
method: function() {}
}).extend({
staticMethod: function() {}
});
.extend({}) 方法是在类上添加静态方法的标准方法。
我唯一不喜欢的是MyClass.staticMethod(); 语法,但没有太多更好的选择。
【讨论】:
JavaScript 中的静态方法是引用它们的对象的属性。它们不会添加到 Object 的原型中。
有两种方法可以在 JavaScript 中向对象添加函数。下面,我向一个名为“MyObject”的虚构对象添加方法。
房产
MyObject.staticMethod = new function() {};
MyObject.staticMethod(); // Call static method.
方法
MyObject.prototype.instanceMethod = new function() {};
new MyObject().instanceMethod(); // Call instance method.
向类添加静态方法的方法有三 (3) 种。以下代码源自 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 添加一个新的mutator
// 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'
【讨论】:
摘自“Pro Javascript with Mootools”:
extend 方法通过
Function.prototype声明并被所有函数继承。因为 MooTools 类本质上是一个函数,所以我们也可以对类使用 extend 方法。该方法与 implementation 方法相似,因为它接受一个对象参数,其中键和值引用将添加的成员。但是,与 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);
}
}
});
【讨论】:
.extend()。