每个 MooTools 开发人员都应该知道的 MooTools 隐藏或晦涩的功能是什么?
请每个答案一个特征。
最佳答案
类修改器
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 异步 Web 应用程序中捕获嵌套的 bugfoot 竞争条件问题很有帮助,否则很难追踪到这些问题。
关于javascript - MooTools 的隐藏功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7999447/