【发布时间】:2021-12-02 15:07:54
【问题描述】:
我正在尝试在 ES6 样式类的实例方法上调用 super,但遇到了 parcel/babel 构建错误。
@parcel/transformer-babel: 'super' is only allowed in object methods and classes. (1291:21)
我的代码实际上看起来像这样
class BaseClass {
constructor(params) {
this.params = params;
}
matches(other) {
// do some matching logic
return true; // or false
}
}
class MyClass extends BaseClass {
matches(other) {
let match = super.matches(other);
// do some additional matching logic
return true; // or false
}
}
错误是当我在MyClass 中调用super.matches(other) 时。
我在super 上找到的所有文档都谈到了它在constructor 中的使用,这很好,以及它在static 方法中的使用,但我没有发现实例方法。
这只是在 JS 中不起作用,是我做错了吗,还是与 babel 转换器或包裹有关?
【问题讨论】:
-
由于代码在现代浏览器中运行良好,因此您的构建过程似乎存在问题。
-
这在 JS 中有效,因此它与 babel 转换器/包裹有关
-
谢谢,我认为你是对的。奇怪的是,我发现相同语法不会导致构建错误的情况,因此需要进一步挖掘。
标签: javascript oop babeljs parceljs