1.21 gigawatts
1.21 gigawatts

Reputation: 17726

Can you use different constructor arguments in an extended class in ES6?

I'm getting warnings in Visual Studio Code when I'm using a constructor with a different arguments. Do the arguments in extended classes need to be the same as the super class?

Class:

class Options {
  constructor(name = null) {
     this.name = name;
  }
}

class ExtendedClass extends Options {
  constructor(colors = null, option = false) {
    if (colors!=null) {
      this.numberOfColors = colors;
    }
    this.option = option;
  }
}

UPDATE:
It looks like using different arguments doesn't matter but that the issue is that a call to super is the problem? Need to test more but it looks like this is the issue:

class ExtendedClass extends Options {
  constructor(colors = null, option = false) {
    super();

    if (colors!=null) {
      this.numberOfColors = colors;
    }
    this.option = option;
  }
}

Upvotes: 0

Views: 974

Answers (1)

D. Pardal
D. Pardal

Reputation: 6587

Do the arguments in extended classes [constructors] need to be the same as the super class [constructor]?

Short answer: no.

However, super() expects the same arguments as the super class constructor. (super() calls the super class constructor with the supplied arguments.)

Upvotes: 1

Related Questions