简体   繁体   English

Javascript多重继承模式

[英]Javascript multiple inheritance pattern

I have used this pattern for inheritance in JS 我已经将此模式用于JS中的继承

MyApp.utils = {
    extend: function (subClass, baseClass, subClassMethods) {
        function inheritance() { }
        inheritance.prototype = baseClass.prototype;
        subClass.prototype = new inheritance();
        subClass.prototype.constructor = subClass;
        subClass.baseConstructor = baseClass;
        subClass.superClass = baseClass.prototype;

        if (subClassMethods == null) return;

        for (var index in subClassMethods) {
            subClass.prototype[index] = subClassMethods[index];
        }
    }
};

You use it like 你用它像

MyApp.utils.extend(SubClass, BaseClass, {
    subMethodOne: function() {        
    }
});

This have worked nicely and the code is pretty straight forward, but it does not work with multiple inheritance because the constructor call will be to the subclass instead of baseclass and that will create a stack overflow 这已经很好地工作了,代码很简单,但是它不能用于多重继承,因为构造函数的调用将是子类而不是基类,这将导致堆栈溢出

WHat is the best pattern for multiple inheritance? 什么是多重继承的最佳模式? It must support prototype and that separate constructor logic in each class 它必须支持原型,并且必须在每个类中使用单独的构造函数逻辑

JSFiddle: http://jsfiddle.net/HcPhL/1/ It will crash FF so beware :D JSFiddle: http : //jsfiddle.net/HcPhL/1/它将使FF崩溃,因此请注意:D

Inheritance in javascript is prototype based. javascript中的继承是基于原型的。 This means objects inherit from each other and not classes. 这意味着对象彼此继承而不是类继承。 Instead of trying to use idioms from other programming languages you know I suggest you get to learn javascript's own idioms. 知道不要推荐使用其他编程语言的惯用语,而是建议您学习javascript自己的惯用语。 I find using modules, cloning, and copying over functionality (since functions are objects), much more useful than classical inheritance in javascript. 我发现使用模块,克隆和复制功能(由于函数是对象),比javascript中的经典继承有用得多。

That said, what you want to do could be accomplished the following way: 就是说,您想做的事情可以通过以下方式完成:

We want A to inherit from B and C sort of objects 我们希望ABC继承对象

  1. Create a new object call it A 创建一个名为A的新对象
  2. Iterate on each property of B (using a for... in loop) and assign it to A. 循环访问B的每个属性(使用for ... in循环)并将其分配给A。
  3. Iterate on each property of C (using a for... in loop) and assign it to A, here you may want to consider what is your take on conflict resolution (meaning whose methods, B or C you want to take precedence. 遍历C的每个属性(使用for ... in循环)并将其分配给A,在这里您可能要考虑对冲突解决有什么看法(这意味着您要优先考虑其方法B或C。

There are plenty of open source javascript libraries with an extend function, you can check the implementations there. 有很多具有扩展功能的开源javascript库,您可以在此处检查实现。 Douglas Crockford also has a basic article on how to accomplish inheritance here and here is an article about javascript multiple inheritance 道格拉斯·克罗克福德(Douglas Crockford)在这里也有关于如何完成继承的基本文章,而这里是有关javascript多重继承的文章

I'm assuming what you're trying to accomplish is code-reuse, I suggest you have a look at this free book by Addy Osmani learning javascript design patterns . 我假设您要完成的工作是代码重用,建议您阅读Addy Osmani的这本免费的书, 学习javascript设计模式 Especially the module pattern. 特别是模块模式。

Use mixins pattern or decorator patterns to support multiple inheritance. 使用mixins模式或装饰器模式来支持多重继承。 I could give you a detailed implementation if you need one 如果需要,我可以给您详细的实施方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM