2

I have Class A and B. I want to inherit both class using Class C. For Class A, i am using below code -

It's working fine for single class. Now i want to inherit class B.

function A () {
}

function B () {
}

function C () {
}

B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;

Now How C inherit A?

//B.prototype = Object.create(A.prototype);
//B.prototype.constructor = B;
Pankaj Bisht
  • 986
  • 1
  • 8
  • 27

2 Answers2

3

An object only has a single prototype chain. Unless there's a relationship between A.prototype and B.prototype, they can't both be on a single object's prototype chain.

You can create a new object that contains a combination of what's on A.prototype and B.prototype and use that as the object for 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;

...but note that:

  1. If you add/remove things on A.prototype or B.prototype afterward, those changes won't be reflected by C.prototype or on instances created via new C.
  2. instanceof will not consider an object created via new C as being instanceof A or instanceof B because A.prototype and B.prototype are not in its prototype chain.
  3. If A.prototype and B.prototype both have the same property (perhaps they both override toString, for instance), you can only have one or the other. (In the code above, B.prototype would win.)
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I am getting a problem on this code you can see on this link - http://stackoverflow.com/questions/35988171/multiple-inheritance-console-output – Pankaj Bisht Mar 14 '16 at 12:57
  • @pankaj98: Yes, but it's a different question. *This* question is answered, as far as I can tell. – T.J. Crowder Mar 14 '16 at 13:01
0

Multiple-Inheritance does not work in JS.

Instead of building convoluted inheritance chains, though, why not try something like Mix-Ins / Traits?

Instead of trying to figure out if WalkingSpeakingPet should inherit from WalkingPet or SpeakingPet, you can instead use Traits, and add them to a pet.

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));
}
Norguard
  • 26,167
  • 5
  • 41
  • 49