LearnFrenzy
Sign in Take a free test

ES6 Features · Find the Error · Question 7

Error: calling a class like a function Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

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

main.js
class User {
  constructor(name) { this.name = name; }
}
const u = User("Asha");
console.log(u.name);
  • A TypeError: Class constructor User cannot be invoked without 'new' Correct
  • B It prints Asha
  • C It prints undefined
  • D ReferenceError
Correct Answer: Option A

Unlike constructor functions, classes refuse to be called as plain functions. Write new User("Asha").

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