【问题标题】:Multi Inheritance in JavaScriptJavaScript 中的多重继承
【发布时间】:2014-09-22 13:09:34
【问题描述】:

为什么这种类型的继承在 JavaScript 中不起作用。有什么方法可以执行此步骤。 请帮忙

function base() {}
base.prototype.findFirst = function() {
    console.log("FindMe First");
}

function base2() {}
base2.prototype.findMe = function() {
    console.log("FindMe ");
}

function inherit() {
    base.call(this);
    base2.call(this);
}

inherit.prototype = base.prototype;
inherit.prototype.constructor = inherit;
inherit.prototype = base2.prototype;
inherit.prototype.constructor = inherit;

var test = inherit();
test.findFirst();
test.findMe();

【问题讨论】:

  • 在 javascript 对象中不能有多个原型链。这是不可能的。
  • 我建议你可能是#doingitwrong。 JS 是一种函数式编程语言,而不是面向对象的语言。您可能最好使用可用的强大功能特性(例如闭包)而不是尝试将 OOP 实现混杂在一起,因为它不会像您想要的那样工作
  • 即使在可能的语言中,多重继承也不是一个好主意。而在 Javascript 中则不是。
  • @Joe JS 是基于原型的,这是另一种 OOP 风格。
  • 好吧,你为什么认为它首先会起作用?你了解 JS 中的继承是如何工作的吗?

标签: javascript inheritance prototype


【解决方案1】:

你正在用 base.prototype 覆盖原型,然后用 base2.prototype。所以它存储 base2.prtotype 即第二个分配的。现在,如果您创建继承类的实例 var test = new inherit(); 你会看到测试有 base2.property 即它在test.property.findMe(); 中有fimeMe() 方法。 为了实现您的目标,您应该尝试扩展或参考 Mixin Multiple Inheritance

【讨论】:

    【解决方案2】:

    Mixins 可用于 javascript 以实现您目前可能希望通过多重继承解决的相同目标。

    【讨论】:

      【解决方案3】:

      我扩展了Function 原型,这不是最好的主意,但可以让您了解它的工作原理。它不是多继承,而是一棵继承树。

      Function.prototype.extend = function(child)
      {
          var childPrototype = child.prototype;
          child.prototype = Object.create(this.prototype);
          for (var property in childPrototype) {
              if (childPrototype.hasOwnProperty(property)) {
                  child.prototype[property] = childPrototype[property];
              }
          }
          child.prototype.constructor = child;
          return child;
      };
      
      var base = (function()
      {
          var base = function()
          {
      
          };
      
          base.prototype.findFirst  = function(x, y)
          {
              console.log("FindMe First");
          };
      
          return base;
      
      }());
      
      var base2 = base.extend(function()
      {
          var base2 = function()
          {
      
          };
      
          base2.prototype.findMe = function(x, y)
          {
              console.log("FindMe ");
          };
      
          return base2;
      
      }());
      
      var inherit = base2.extend(function()
      {
          var inherit = function()
          {
      
          };
      
          return inherit;
      
      }());
      
      var test = new inherit();
      test.findFirst();
      test.findMe();
      

      【讨论】:

      • 我认为您应该删除 childPrototype.hasOwnProperty(property) 测试 - 否则多级继承无法正常工作。
      • 既然OP似乎不知道他自己的代码是做什么的,你介意用文字而不是代码来解释你的想法吗?
      • @Bergi 看看 here The use of hasOwnProperty() prevents the loop from enumerating over any inherited properties on the object
      • 我知道它的作用,我只是告诉你我们可能在这里想要它......
      • @Bergi 我已经测试过了,这可能会导致意想不到的结果。例如,如果您向对象添加一个属性:Object.prototype.test = 'Object';,则无法使用base.prototype.test = 'base'; 覆盖它。请自行测试..
      猜你喜欢
      • 2011-04-15
      • 2018-06-24
      • 1970-01-01
      • 2015-10-14
      • 2013-09-01
      • 2013-01-12
      • 1970-01-01
      相关资源
      最近更新 更多