简体   繁体   English

javascript - Object.create 多个原型

[英]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编辑:我在 CHROME 中使用 THREE.JS

Javascript doesn't have multiple inheritance, the only way to do it is just extending base class. Javascript 没有多重继承,唯一的方法就是扩展基类。 Take a look at sample below, it's a bit adopted from MDN Object.create看看下面的示例,它有点来自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');
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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