每个MooTools开发人员都应该知道MooTools的隐藏或晦涩的特性是什么?
请为每个答案提供一个功能。
发布于 2011-11-04 06:03:50
类变更器
MooTools有一个很棒的特性,它允许您创建自己的类变更器。例如,要为被引用的特定类方法添加记录器,您可以执行以下操作:
// define the mutator as 'Monitor', use as Mointor: ['methodname', 'method2'...]
Class.Mutators.Monitor = function(methods){
if (!this.prototype.initialize) this.implement('initialize', function(){});
return Array.from(methods).concat(this.prototype.Monitor || []);
};
Class.Mutators.initialize = function(initialize){
return function(){
Array.from(this.Monitor).each(function(name){
var original = this[name];
if (original) this[name] = function() {
console.log("[LOG] " + name, "[SCOPE]:", this, "[ARGS]", arguments);
original.apply(this, arguments);
}
}, this);
return initialize.apply(this, arguments);
};
};然后在课堂上:
var foo = new Class({
Monitor: 'bar',
initialize: function() {
this.bar("mootools");
},
bar: function(what) {
alert(what);
}
});
var f = new foo();
f.bar.call({hi:"there from a custom scope"}, "scope 2");试试jsfiddle:http://jsfiddle.net/BMsZ7/2/
这个小gem对我在HUUUGE async webapp中捕获嵌套的bugfoot竞态条件问题很有帮助,否则很难追踪到这些问题。
发布于 2011-11-04 03:33:51
Function.prototype.protect可能是一个鲜为人知的好东西。
用于在类中包含受保护的方法:
var Foo = new Class({
fooify: function(){
console.log('can\'t touch me');
}.protect(),
barify: function(){
this.fooify();
}
});
var foo = new Foo();
foo.fooify(); // throws error
foo.barify(); // logs "can't touch me"就我个人而言,我并不经常使用它,但在某些情况下它可能会很有用。
发布于 2011-11-04 03:35:05
Function.prototype.overloadGetter和Function.prototype.overloadSetter
请看这篇文章:What does MooTools' Function.prototype.overloadSetter() do?
https://stackoverflow.com/questions/7999447
复制相似问题