javascript - MooTools 的隐藏功能

标签 javascript mootools

每个 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/

相关文章:

jquery - MooTools 中的事件绑定(bind)(如 jQuery)

javascript - 使用 jQuery 和 Mootools

javascript - 将 onChange 与 React 一起使用时,未注册空格

javascript - extjs 中日期选择器的定位

javascript - 如何按顺序给出数字,即使我删除一行输入,顺序也不应该重复

javascript - Fancyupload 的更新替代品或更新版本?

javascript - 如何仅向 ul 列表的一个元素添加样式

javascript - 放置在 head 标记中时 Jquery 代码不起作用

jquery - MochaUI教程

javascript - 加载 XML 和 JS 以严格顺序处理它