class - Mootools "Extends"加 "Implements"

标签 class mootools extends

我喜欢编写简洁而性感的代码(在性能和内存方面),我正在使用 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 */}
})

如何判断每次扩展 secondaryclass 时是否不会创建已实现类的新副本? 我做上面所做的事情的原因是为每个需要它的类扩展 SecondClass - 静态地这样做,而第二个类不能扩展多个类,因此我使用 Implements。

最佳答案

The main difference between Extends and Implements is that Implement changes the class's prototype, while Extend creates a copy. This means that if you implement a change into a class all instances of that class will inherit that change instantly, while if you use Extend then all existing instances will remain the same.

这是来自 mootorial 的引用,请查看。 http://mootorial.com/wiki/mootorial/02-class/#implement-vs.-extend

至于测试 - 我强烈建议您使用 ninja 类构建一些示例案例并将它们放在 http://www.jsfiddle.net 上- 然后在 google 或 IRC (irc.freenode.net#mootools) 上寻求一些分析建议或 mootools 邮件列表,所以似乎没有从 mootools 核心团队获得很多点击。理想情况下,您想与 aaron newton、arian、cpojer 或 rpflo 等人交谈:)

<小时/>

更新:我什至在博客中提到过这一点,但我错了。只是引入 ExtendsImplements 之类的变元的顺序有所不同。您可以实现和扩展,但需要先声明 Extends 才能工作。

在此处了解更多信息:http://fragged.org/mootools-pattern-fun-class-implements-extends-at-the-same-time_1359.html

<小时/>

更新 事实证明,在某些情况下这很有用。问题是这样的:

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.

...根本不起作用。您需要类人类来实现ninja,而不是类badass来简单地扩展人类。除了人类获得新的kill方法的副作用(他们可能知道也可能不知道)之外,这意味着坏蛋将能够使用.kill并调用他的直接 parent 人类。

为什么不按照你想要的方式重写东西并且没有复杂性呢?因为您可能正在扩展像 Request.JSONP 这样的 native 类,然后决定将新的存储类混合到您的扩展类中。真实的故事......无论哪种方式,您可能都没有能力重构某些可用的类。

一个有趣的模式来克服这个问题(考虑你的 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 }); 但类可以有更多功能。

如果您想保存对 ninja 类的引用以供其他用途,则上面的内容与此相同(如果您打算重用 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!

希望这对某人有帮助。这是一个实际示例,我使用附加存储类作为 mixin 来扩展 Request.JSONP:http://jsfiddle.net/dimitar/YacZe/

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

相关文章:

javascript - Mootools 破坏了我的全局变量!!!谷歌地图 + Mootools

Python 从属性移动到他的类

PHP 分配另一个类的 $this

java - ClassLoader.getSystemResourceAsStream(className) 尝试加载类文件资源时返回 null

java - 在 Java 类型参数中,<? extends E> 仅表示严格的子类型?还是 E 也足够?

java - 创建一个扩展 keyListener 的类

java - Collection <?扩展 T> 与集合 <T>

ruby - 在 Ruby 中调用另一个类中的一个类中的方法

javascript - MooTools 类和 JsDoc

css - mootools 可以更改 css .class 的属性,以便更改反射(reflect)在属于该类的所有元素上吗?