Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
Javascript Class ReferenceError

It's about JS class from MDN page. I don't understand why Bad class has a reference error. Is it because empty constructor of Bad class calls super() as a default?

class Base {}

class Good extends Base {}

class AlsoGood extends Base {

  constructor() {

   return {a: 5};

   }

}

class Bad extends Base {

  constructor() {}

}

new Good();

new AlsoGood();

new Bad(); // ReferenceError
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

To avoid getting this error you must either remove constructor from the class or call super() inside the constructor of the Bad class

about 4 years ago · Juan Pablo Isaza Relatório

0

Is it because empty constructor of Bad class calls super() as a default?

No - if the empty constructor did call super by default, you wouldn't be seeing the error.

A derived class's constructor, if it exists, must call the parent's constructor with super.

If a derived class lacks any constructor at all, super will be called automatically. That is

class Good extends Base {
}

is equivalent to

class Good extends Base {
  constructor(...args) {
    super(...args);
  }
}

The same does not extend to empty constructors - if the constructor is present, super does not get called automatically - but super must be called.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda