foundation
Classes and objects
Explain classes as blueprints for object state and behavior, and instances as concrete objects with identity, lifecycle, and invariants.
A class is a blueprint describing state (fields) and behavior (methods); an object is a concrete instance with identity, lifecycle, and invariants enforced at construction. In JS/TS, classes are syntactic sugar over prototypes—`new User('Ada')` creates an instance with its own property storage linked to shared methods on the prototype chain.
Distinguish type (class/interface) from instance (object in memory). Identity matters when you track entities by ID, not merely by field values.
On interviews: explain constructor invariants, why two objects with equal fields are not the same entity, and how this maps to prototypes in JavaScript.
Common pitfalls: treating classes as namespaces of static functions, mutable singletons posing as objects, and confusing class references with instances.
The trade-off is modeling clarity versus boilerplate—use classes when lifecycle and identity are first-class.
Checklist:
- Define class versus instance versus value.
- Name invariants enforced in constructor.
- Relate JS class syntax to prototypes.