我想写我的代码 slim 又性感(在性能和内存方面),我正在使用Mootools,想知道我是否以正确的方式使用它,还可以通过告诉我如何测试我的代码来找到我,以帮助我。答案我正在寻找自己的自我。

//First we create a class like so:

var FirstClass = new Class {(
   'someFunc': function() { /* do stuff */ }
})

//Now this class uses first class with "Implements"

var SecondClass = new Class ({
   'Implements': [FirstClass, SomeOtherClass, SomeOtherOtherClass],
   'iAlsoDoStuff': function() {/*do stuff */}
})

// finally the class that Extends second class
var ThirdClass = new Class ({
   'Extends': SecondClass,
   'takeOverTheWorld': function() {/*code to win lottery */}
})

我如何知道是否每次扩展secondclass时都不会对Implemented类进行新的复制?
我执行上述操作的原因是为每个需要它的类扩展SecondClass-静态地这样做,而第二个类不能再扩展一个类,因此我正在使用Implements。

最佳答案


这是来自Mootorial的报价,请查看。 http://mootorial.com/wiki/mootorial/02-class/#implement-vs.-extend
至于测试-我非常建议您使用忍者类构建一些示例案例,并将其放入http://www.jsfiddle.net-然后在Google或IRC上寻求一些分析建议或mootools邮件列表(irc.freenode.net#mootools) ,SO似乎没有从mootools核心团队中获得很多成功。理想情况下,您想与aaron newton,arian,cpojer或rpflo之类的人交谈:)

更新:我什至在博客上写了这个,但是我错了。像ExtendsImplements之类的变体的引入顺序完全不同。可以实现和扩展,但是需要首先声明Extends才能起作用。
在这里阅读更多:http://fragged.org/mootools-pattern-fun-class-implements-extends-at-the-same-time_1359.html

update 事实证明,在某些情况下这很有用。这是问题所在:

var ninja = new Class({
    kill: function() {
        alert("kill!");
    }
});

var human = new Class({
    initialize: function(){
        alert("i r human!");
    }
});

var badass = new Class({
    Implements: [ninja],
    Extends: human,
    initialize: function() {
        alert("i r badass and.. ");
        this.parent();
        this.kill();
    }
});

new badass(); // i r badass, i r human, this.kill is not a function exception.
...根本行不通。您需要人工类来实现忍者,而 badass 则可以简单地扩展人工。除了人类获得一种新的杀死方法(他们可能知道或可能不知道)的副作用之外,这还意味着Badass将能够使用.kill并呼吁其直接父级人类。
为什么不按照您想要的方式重写这些内容,而又不复杂呢?因为您可能要扩展 native 类(例如Request.JSONP),然后决定将一个新的存储类混入扩展的类中。真实的故事...无论哪种方式,您都可能无法重构某些可用的类。
克服这一问题的有趣模式(考虑人类类request.jsonp,在其他地方定义)-如果您只想向要扩展的类添加更多方法和属性,但不打算重用mixin类(忍者):
human.implement(new new Class({
    kill: function() {
        alert("kill!");
    }
}));

var badass = new Class({
    Extends: human,
    initialize: function() {
        alert("i r badass and.. ");
        this.parent();
        this.kill();
    }
});

new badass(); // // i r badass, i r human, kill!
可以说,您可以只做human.implement({ method: function });,但一个类可以做得更多。
如果您想保存对忍者类的引用以用于其他用途,则以上内容将与此相同(如果您计划重复使用mixin):
var ninja new Class({
    kill: function() {
        alert("kill!");
    }
});

human.implement(new ninja);
// this is what differs from say - instantiation + shared inherited properties.
// also, a constructor will work.
// the alternative would just do:
// human.prototype.kill = function() { alert("kill"); }

var badass = new Class({
    Extends: human,
    initialize: function() {
        alert("i r badass and.. ");
        this.parent();
        this.kill();
    }
});

new badass(); // // i r badass, i r human, kill!
希望这对某人有帮助。这是一个实际的示例,其中我在扩展Request.JSONP时使用了其他存储类作为混合:http://jsfiddle.net/dimitar/YacZe/

关于class - Mootools "Extends"加上 "Implements",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1720931/

10-12 20:29