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;
2
  • hope this helps stackoverflow.com/questions/9163341/… Commented Mar 11, 2016 at 7:08
  • Note: In your example, you're missing the necessary call to A from C: A.call(this); Commented Mar 11, 2016 at 7:27

2 Answers 2

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.)
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting a problem on this code you can see on this link - stackoverflow.com/questions/35988171/…
@pankaj98: Yes, but it's a different question. This question is answered, as far as I can tell.
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));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.