解决方法
类变异器
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竞争条件问题,否则会很难跟踪.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。