ES6 多重继承

ES6 multiple inheritance?

本文关键字:多重继承 ES6      更新时间:2023-09-26

有人可以解释一下以下有效ES6代码的含义吗?

'use strict';
class first {
    constructor() {
    }
}
class second {
    constructor() {
    }
}
class third extends (first, second) {
    constructor() {
        super();
    }
}

据我所知,JavaScript 中没有多重继承,但示例中所示的语法不会引发任何错误(在 Node.js 4.3.0 中(,并且它有效,... -我试图理解的内容如何,或者它在那里到底做了什么......

另外,我注意到如果我注释掉super()调用,那么代码就会开始抛出错误ReferenceError: this is not defined

这只是逗号运算符的一个令人困惑的情况。

(1, 2) // 2
(1, 2, 3) // 3
(first, second) // second

它不会抛出错误,因为它是有效的语法,但它在你期望的意义上也不起作用third只会继承second.

我们可以通过编译为 ES5 语法来验证是否是这种情况,以检查它是否不会影响类定义。

var third = (function (_ref) {
  _inherits(third, _ref);
  function third() {
    _classCallCheck(this, third);
    _get(Object.getPrototypeOf(third.prototype), 'constructor', this).call(this);
  }
  return third;
})((first, second));

评估(first, second)的结果作为_ref传入,然后从中third _inherits