简体   繁体   中英

javascript - Object.create more than one prototype

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

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');
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM