首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MooTools的隐藏功能

MooTools的隐藏功能
EN

Stack Overflow用户
提问于 2011-11-04 01:41:03
回答 6查看 548关注 0票数 9

每个MooTools开发人员都应该知道MooTools的隐藏或晦涩的特性是什么?

请为每个答案提供一个功能。

EN

回答 6

Stack Overflow用户

发布于 2011-11-04 06:03:50

类变更器

MooTools有一个很棒的特性,它允许您创建自己的类变更器。例如,要为被引用的特定类方法添加记录器,您可以执行以下操作:

代码语言:javascript
复制
// 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);
    };
};

然后在课堂上:

代码语言:javascript
复制
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 async webapp中捕获嵌套的bugfoot竞态条件问题很有帮助,否则很难追踪到这些问题。

票数 9
EN

Stack Overflow用户

发布于 2011-11-04 03:33:51

Function.prototype.protect可能是一个鲜为人知的好东西。

用于在类中包含受保护的方法:

代码语言:javascript
复制
var Foo = new Class({

    fooify: function(){
        console.log('can\'t touch me');
    }.protect(),

    barify: function(){
        this.fooify();
    }

});

 var foo = new Foo();
 foo.fooify(); // throws error
 foo.barify(); // logs "can't touch me"

就我个人而言,我并不经常使用它,但在某些情况下它可能会很有用。

票数 6
EN

Stack Overflow用户

发布于 2011-11-04 03:35:05

Function.prototype.overloadGetterFunction.prototype.overloadSetter

请看这篇文章:What does MooTools' Function.prototype.overloadSetter() do?

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7999447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档