簡體   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