ES6类多重继承 [英] ES6 Class Multiple inheritance

查看:251
本文介绍了ES6类多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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() {
    }
}

将多个类扩展到新类?如果是这样,解释器是否比ClassOne更喜欢ClassTwo的方法/属性?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆