AceDevHub
Intermediate JavaScript Interview QuestionsIntermediateComparison

JavaScript · Question 42

How are JavaScript classes related to constructor functions and prototypes?

Direct answer

JavaScript classes provide structured syntax and additional semantics on top of the language’s prototype-based object model; instance methods still live on the class prototype and inheritance still uses prototype links.

It is fair to say classes are built on the prototype model, but “classes are only syntax sugar” can hide meaningful behavioral rules. A class declaration creates a constructor and a prototype object, and methods declared in the class body are placed on that prototype.

  • Class constructors must be called with new; calling them as ordinary functions throws.
  • Class bodies run in strict mode.
  • Class methods are non-enumerable by default, unlike methods assigned with simple property assignment to a prototype.
  • extends and super provide standardized inheritance and parent-constructor behavior.
  • Private fields such as #count are language-enforced private state, not prototype properties.

For interview reasoning, treat class User {} as a cleaner way to define constructor/prototype relationships while remembering that classes also impose their own syntax and runtime rules.