解决方法
类变异器
MooTools有一个很好的功能,可以让您创建自己的Class mutator.例如,为了引用特定的类方法添加一个记录器,你可以这么做:
// 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/
这个小宝石一直有助于我在一个HUUUGE异步webapp中捕获嵌套的bugfoot竞争条件问题,否则会很难跟踪.