intermediate

Classes and constructors

Class syntax over prototypes, constructor rules, fields, inheritance, and private members.

Classes are syntactic sugar over prototypes with stricter rules. `extends` and `super` wire up inheritance. Derived constructors must call `super()` before using `this`.

Methods are on the prototype (shared). Class field arrow functions create per-instance functions. Private fields (`#id`) are not normal properties.

JavaScript classes differ from Java/C#: no true privacy without `#`, no overloading, methods are not auto-bound.

On interviews, explain the concept with a concrete example and name the runtime behavior interviewers probe.

Common pitfalls include mixing similar APIs and forgetting edge cases during live coding.

The trade-off is often clarity versus performance or safety versus convenience.

Checklist:

  • Mention prototype method sharing.
  • Call super before this in derived constructors.
  • Bind methods or use fields intentionally.