Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!