JavaScript - super() 关键字意外

JavaScript - super() keyword unexcepted

我遇到了一个小问题,我希望有人能帮助我快速解决这个问题。这是我的代码:

class Client {
    /**
   * @param {ClientOptions} [options] Options for the client
   */
  constructor(options = {}) {
    super();
    this.lnk = "https://marketpala.glitch.me";
    this.endpoints = [
        "/commandes.json",
        "/users.json",
        "/blacklist.json",
        "/infov.json",
        "/reserved.json"
    ]
  }
}

这里是导出客户端的代码:

module.exports = {
    Client: require('./client/Client')
}

这是我用来测试客户端的代码:

const tst = require('./palamazon')
let t = new tst.Client()

这是我得到的错误:

super();
    ^^^^^

SyntaxError: 'super' keyword unexpected here

希望有人能帮助我!

(我在 javascript 中编码)

Super 关键字用于继承的 classes 以在子 class 中使用它们的属性。您的 class 未从任何其他 class 扩展,因此 super 未被接受。

By calling the super() method in the constructor method, we call the parent's constructor method and gets access to the parent's properties and methods:

如果您的 class 未从任何其他 class 扩展,则必须删除 super 方法。

检查一些细节here

因为你没有扩展任何 class 所以 super 不是预期的,super 调用父 class 构造函数但在这种情况下没有父 class.

super() 用于在从另一个 class 扩展一个 class 时调用父 class 上的原始方法,

class MyFirstTestClass {
    constructor() {
        console.log("Hello");
    }
}

class MySecondTestClass extends MyFirstTestClass {
    constructor() {
        super();

        console.log("World");
    }
}

const test = new MySecondTestClass();

这将输出 Hello 然后 World。如果不调用 super(),只会输出 World,因为第二个 class 的构造函数会覆盖第一个的构造函数。

您编写的 class 不是从另一个 class 扩展而来的,因此 super() 没有可调用的父 class。

如果您的意图是从另一个 class 继承,或者删除您调用 super().

的行,您应该能够解决您的问题。