Class Declaration Syntax in JavaScript

Class declarations provide a class-oriented way to define constructors and methods that are placed on the class prototype. They make prototype-based object behavior easier to express and read.

A class declaration uses the `class` keyword followed by a class name and a body: `class Person { ... }`. A `constructor()` method runs when an instance is created with `new` and is commonly used to initialize instance properties.

Methods defined directly inside a class body become methods on the class's `prototype`, rather than being recreated as separate functions for every instance. For example, `greet()` is shared by instances through `Person.prototype.greet`.

A class declaration is not instantiated automatically. You create an object with `new Person(...)`. Each resulting object can have its own instance data while sharing methods through the prototype chain.

Classes also support inheritance with `extends` and can call a parent constructor or method using `super`. For junior-level interviews, the important distinction is that JavaScript classes provide syntax over JavaScript's existing prototype-based object model rather than introducing a separate inheritance mechanism.

Key points

Interview questions

What does the constructor do in a class?

The `constructor()` method is called when an object is created with `new`. It commonly assigns initial values to the new instance through `this`. A class can have at most one constructor method. If no constructor is declared, JavaScript provides a default constructor behavior.

How do you declare a class in JavaScript?

Use the `class` keyword followed by a class name and a block. Put a `constructor()` method inside the block when instances need initialization. Other methods can be declared directly in the class body. Instances are then created with `new`.

Where are methods defined in a JavaScript class stored?

Ordinary methods declared in a class body are placed on the class's `prototype`. Instances access those methods through the prototype chain. This means the method does not normally need to be created separately for every instance. Instance-specific properties assigned with `this` are different because they belong directly to each instance.

Are JavaScript classes really different from prototypes?

JavaScript remains prototype-based even when class syntax is used. A class declaration provides a more structured syntax for defining a constructor and prototype methods. Instances still inherit behavior through the prototype chain. Therefore, understanding prototypes remains important when debugging or explaining class behavior.

What is the difference between a class method and an arrow function assigned in the constructor?

A normal class method is placed on the prototype and is shared by instances. An arrow function assigned in the constructor becomes an own property of each instance. Arrow functions also capture lexical `this`, while a normal method gets `this` from how it is called. This can make instance arrow functions useful as callbacks when preserving the instance context is important.