koro
koro

Reputation: 111

JavaScript - super() keyword unexcepted

I'm getting in trouble with a small problem and I wish that somebody can help me resolving this fast. Here is my code :

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"
    ]
  }
}

Here is the code to export the client :

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

Here is the code I use to test my client :

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

And here is the error I get :

super();
    ^^^^^

SyntaxError: 'super' keyword unexpected here

Hope somebody can help me!

(I'm coding in javascript)

Upvotes: 1

Views: 253

Answers (3)

Michael
Michael

Reputation: 1185

super() is used to call the original method on the parent class when you are extending one class from another like so,

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

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

        console.log("World");
    }
}

const test = new MySecondTestClass();

This will output Hello and then World. Without calling super(), only World would be outputted because the constructor on the second class would override the constructor on the first.

The class you wrote does not extend from another class, and so super() has no parent class to call to.

You should be able to fix your problem by either inheriting from another class if that was your intention, or by simply deleting the line where you call super().

Upvotes: 0

Nitheesh
Nitheesh

Reputation: 19986

Super keyword is used in inherited classes to use their properties inside the child class. Your class is not extended from any other classes, so super is not accepted.

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:

If your class is not being extended from any other classes, the super method must be deleted.

Check some details here.

Upvotes: 4

Ajay Kumar
Ajay Kumar

Reputation: 97

As you are not extending any class so the super is not expected, super calls parent class constructor but in this case there is no parent to this class.

Upvotes: 0

Related Questions