1

I want my child object to inherit prototype of more than one parent, this doesnt work:

child.prototype = Object.create(parent1.prototype, parent2.prototype);

and also this:

child.prototype = Object.create(parent1.prototype);
child.prototype.add(Object.create(parent2.prototype));

any suggestions ?

EDIT : Im using THREE.JS with CHROME

RoeePeleg
  • 31
  • 8
  • 2
    Possible duplicate of [Multiple inheritance/prototypes in JavaScript](http://stackoverflow.com/questions/9163341/multiple-inheritance-prototypes-in-javascript) – Hanky Panky Mar 23 '16 at 07:51
  • http://jsfiddle.net/arunpjohny/mef162yb/1/ – Arun P Johny Mar 23 '16 at 07:57
  • 1
    What Arun P Johny has linked is multi-level inheritance. You can't do multiple inheritance the way you've described it in JavaScript like you can do in say C++. Can you give more information as to why you're wanting multiple inheritance and maybe we might be able to give an OM that will achieve the same result without requiring multiple inheritance. – Chris Tomich Mar 23 '16 at 07:59
  • @ArunPJohny Worth posting it in an answer I would say. Leaving a fiddle without any comment/explanation is not so useful for people ending up here. – Wilt Mar 23 '16 at 09:31
  • This is not a three.js question. – Wilt Mar 23 '16 at 09:33

1 Answers1

1

Javascript doesn't have multiple inheritance, the only way to do it is just extending base class. Take a look at sample below, it's a bit adopted from MDN Object.create

function SuperClass(){
  //class 1 - has some super staff
  console.log('SuperClass');
}
function OtherSuperClass(){
  //class 2 - has some other super staff
  console.log('OtherSuperClass');
}

//MyClass wants to inherit all supers staffs from both classes
function MyClass() {
  SuperClass.call(this);
  OtherSuperClass.call(this);
}

//inheritance and extension
//extend class using Object.assign
MyClass.prototype = Object.create(SuperClass.prototype); // inheritance
Object.assign(MyClass.prototype, OtherSuperClass.prototype); // extension

//define/override custom method
MyClass.prototype.myMethod = function() {
  console.log('double inheritance method');
};
Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69