LearnFrenzy
Sign in Take a free test

ES6 Features · Find the Error · Question 8

Error: this before super() Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
class Vehicle {
  constructor(wheels) { this.wheels = wheels; }
}
class Bike extends Vehicle {
  constructor(brand) {
    this.brand = brand;
    super(2);
  }
}
console.log(new Bike("Hero").brand);
  • A ReferenceError: super() must be called before this is used in a derived constructor Correct
  • B It prints Hero
  • C TypeError
  • D SyntaxError
Correct Answer: Option A

In a derived class the object is created by the parent constructor, so this exists only after super() returns. Swap the two lines.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links