ES6 类多重继承 [英] ES6 Class Multiple inheritance
问题描述
我已经在 BabelJS 和 MDN(根本没有任何信息),但如果我在四处寻找时不够小心,请随时告诉我有关 ES6 规范的更多信息.
I've done most of my research on this on BabelJS and on MDN (which has no information at all), but please feel free to tell me if I have not been careful enough in looking around for more information about the ES6 Spec.
我想知道 ES6 是否像其他鸭子类型的语言一样支持多重继承.例如,我可以执行以下操作:
I'm wondering whether or not ES6 supports multiple inheritance in the same fashion as other duck-typed languages do. For instance, can I do something like:
class Example extends ClassOne, ClassTwo {
constructor() {
}
}
将多个类扩展到新类?如果是这样,解释器会更喜欢 ClassTwo 的方法/属性而不是 ClassOne 吗?
to extend multiple classes on to the new class? If so, will the interpreter prefer methods/properties from ClassTwo over ClassOne?
推荐答案
一个对象只能有一个原型.可以通过创建一个父对象作为两个父原型的组合来继承两个类.
An object can only have one prototype. Inheriting from two classes can be done by creating a parent object as a combination of two parent prototypes.
子类化的语法使得可以在声明中做到这一点,因为 extends
子句的右侧可以是任何表达式.因此,您可以根据自己喜欢的任何标准编写一个组合原型的函数,并在类声明中调用该函数.
The syntax for subclassing makes it possible to do that in the declaration, since the right-hand side of the extends
clause can be any expression. Thus, you can write a function that combines prototypes according to whatever criteria you like, and call that function in the class declaration.
这篇关于ES6 类多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!