繁体   English   中英

javascript - Object.create 多个原型

[英]javascript - Object.create more than one prototype

我希望我的子对象继承多个父对象的原型,这不起作用:

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

还有这个:

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

有什么建议 ?

编辑:我在 CHROME 中使用 THREE.JS

Javascript 没有多重继承,唯一的方法就是扩展基类。 看看下面的示例,它有点来自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