javascript – MooTools的隐藏功能

MooTools的每个MooTools开发人员应该注意什么隐藏或者模糊的功能

每个答案有一个功能.

解决方法

类变异器

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");

尝试jsfiddlehttp://jsfiddle.net/BMsZ7/2/

这个小宝石一直有助于我在一个HUUUGE异步webapp中捕获嵌套的bugfoot竞争条件问题,否则会很难跟踪.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...