JavaScript Classes
JavaScript classes provide a convenient syntax for creating objects, defining methods, running initialization logic, and expressing inheritance. They are built on JavaScript's existing prototype-based object model rather than replacing it.
A class declaration defines a class body that can contain a `constructor` and methods. Calling the class with `new` creates an object, initializes it through the constructor, and establishes the appropriate prototype relationship.
Methods defined in a class are normally placed on the class's `prototype` rather than copied onto every instance. This means instances can share the same method implementation while retaining their own instance properties.
The `extends` keyword creates inheritance between classes, while `super()` invokes the parent constructor and `super.method()` can call an inherited method. A derived constructor must call `super()` before it can use `this`.
Classes also support encapsulation features such as private fields and methods using `#`. These are genuinely private at the language level: code outside the class cannot directly access a private member. Class syntax does not make JavaScript inheritance fundamentally class-based; prototypes remain the underlying mechanism.
Key points
- Use `new` to create an instance from a class.
- The `constructor` initializes instance state.
- Class methods are generally stored on the class's `prototype`.
- Use `extends` and `super` for class inheritance.
- Private fields and methods can use the `#` syntax.
Interview questions
What does the `constructor` method do?
The `constructor` runs when an instance is created with `new`. It is commonly used to initialize instance properties such as a name or configuration value. Each instance gets its own initialized state. A class can have at most one constructor method.
What is a JavaScript class, and how does it relate to prototypes?
A class is syntax for defining objects, constructors, methods, and inheritance in a structured way. JavaScript classes still use prototypes underneath. Normal methods defined in a class are placed on the class prototype and are therefore shared by instances. So classes provide a class-like interface without replacing JavaScript's prototype system.
How does inheritance work with `extends` and `super`?
`extends` establishes an inheritance relationship between a derived class and a base class. In a derived constructor, `super()` invokes the parent constructor and must happen before using `this`. `super.method()` can invoke a method defined by the parent class. The resulting prototype chain lets instances access inherited behavior.
Are JavaScript classes truly class-based internally?
No. JavaScript remains prototype-based internally. Class syntax provides a more structured way to express constructors, methods, and inheritance, but objects still delegate property lookup through prototypes. Understanding this explains behaviors such as shared prototype methods and prototype-chain inheritance.
How would you implement private state in a class?
Modern JavaScript supports private fields using the `#` prefix. For example, `#count` can be declared inside a class and accessed only by code belonging to that class. External code cannot read it through `object.#count` or ordinary property lookup. This differs from a convention such as `_count`, which is merely a naming convention rather than language-enforced privacy.