Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中继承2类_Javascript - Fatal编程技术网

如何在javascript中继承2类

如何在javascript中继承2类,javascript,Javascript,我有A类和B类。我想使用C类继承这两个类。对于A类,我使用以下代码- 这对一个班来说很好。现在我想继承类B function A () { } function B () { } function C () { } B.prototype = Object.create(A.prototype); B.prototype.constructor = B; 现在C如何继承A //B.prototype = Object.create(A.prototype); //B.prototype.

我有A类和B类。我想使用C类继承这两个类。对于A类,我使用以下代码-

这对一个班来说很好。现在我想继承类B

function A () {
}

function B () {
}

function C () {
}

B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;
现在C如何继承A

//B.prototype = Object.create(A.prototype);
//B.prototype.constructor = B;
一个对象只有一个原型链。除非
a.prototype
B.prototype
之间存在关系,否则它们不能同时位于单个对象的原型链上

您可以创建一个新对象,该对象包含
a.prototype
B.prototype
上的内容的组合,并将其用作
C.prototype
的对象:

function C() {
    A.call(this);
    B.call(this);
}
C.prototype = Object.assign({}, A.prototype, B.prototype); // Object.assign is
                                                           // ES2015, but can be
                                                           // shimmed
C.prototype.constructor = C;
……但请注意:

  • 如果之后在
    A.prototype
    B.prototype
    上添加/删除内容,则
    C.prototype
    或通过
    new C
    创建的实例不会反映这些更改
  • >代码/<代码>将不会考虑通过<代码>新C < /代码>创建的对象为<<代码> > 或代码>实例,因为<代码> A原型和<代码> B.prototype <代码>不在它的原型链中。
  • 如果
    A.prototype
    B.prototype
    都具有相同的属性(例如,它们可能都覆盖
    toString
    ),则只能具有其中一个属性。(在上面的代码中,
    B.prototype
    将获胜。)

  • 多重继承在JS中不起作用

    但是,与其构建复杂的继承链,为什么不尝试像mixins/Traits这样的东西呢

    您可以使用特征,并将它们添加到宠物中,而不是试图确定
    WalkingSpeakingPet
    是否应该继承自
    WalkingPet
    SpeakingPet

    function Walker (target) {
      return {
        walk (x, y) { console.log("I'm walking!"); }
      };
    }
    
    function Flyer (target) {
      return {
        fly (x, y, z) { console.log("I'm flying!"); }
      };
    }
    
    function Swimmer (target) {
      return {
        swim (x, y, z) { console.log("I'm swimming!"); }
      };
    }
    
    
    function Pet (type, name) {
      return { type, name };
    }
    
    function Dog (name) {
      const dog = Pet("Dog", name);
      return Object.assign(dog, Walker(dog));
    }
    
    function Parakeet (name) {
      const parakeet = Pet("Bird", name);
      return Object.assign(parakeet, Flyer(parakeet));
    }
    
    function HarveyTheWonderHamster () {
      const harvey = Pet("Hamster", "Harvey");
      return Object.assign(harvey, Walker(harvey), Flyer(harvey), Swimmer(harvey));
    }
    

    希望这有助于注意:在您的示例中,您错过了从
    C
    A
    的必要调用:
    A.call(this)我在这个代码上遇到了一个问题,你可以在这个链接上看到-@pankaj98:是的,但这是一个不同的问题。据我所知,这个问题已经得到了回答。