JavaScript Constructor Functions

Constructor functions are regular JavaScript functions designed to create and initialize objects when called with `new`. Instances can share methods through the constructor function’s `prototype`, avoiding a separate copy of each method per object.

A constructor function is commonly named with an initial capital letter and uses `this` to initialize properties on a newly created object. Calling it with `new`, such as `new Person("Ada")`, creates an instance and binds `this` to that new object.

The `new` operation also connects the instance to the constructor's `prototype`. This means methods placed on `Person.prototype` can be shared by all `Person` instances instead of being recreated for every object.

For example, `Person.prototype.greet = function () { ... }` defines one shared method. An instance can access it through the prototype chain even though `greet` is not an own property of the instance.

Constructor functions are an older object-oriented pattern that remains important for understanding JavaScript's prototype system. ES2015 classes provide a more familiar syntax, but class instances still use prototypes for instance methods.

Key points

Interview questions

What happens when you call a function with `new`?

JavaScript creates a new object and uses that object as `this` during the constructor call. The new object's internal prototype is connected to the constructor's `prototype`. The constructor can initialize properties on the object through `this`. Finally, the resulting object is returned unless the constructor explicitly returns a non-primitive object.

Why put methods on a constructor's prototype?

A prototype method can be shared by every instance created from the constructor. This avoids allocating a separate function value for each instance. Property lookup checks the instance first and then follows its prototype chain when necessary. This makes prototypes useful for shared behavior.

What is the difference between an instance property and a prototype property?

An instance property is stored directly on the object, such as `this.name` created inside a constructor. A prototype property is stored on the object's prototype and can be inherited by instances. If both the instance and prototype contain the same property name, the instance property is found first. This behavior is part of JavaScript's prototype-chain lookup.

What can go wrong when a constructor function is called without `new`?

Without `new`, the function does not automatically create an instance or establish the constructor's prototype relationship. In strict mode, `this` inside a plain function call is `undefined`, so assigning `this.name` can throw an error. In non-strict code, `this` may refer to the global object, potentially causing unintended global mutations. A common defensive technique is to use `new.target` when appropriate or prefer class syntax when its semantics fit the design.

How do constructor functions relate to JavaScript classes?

Classes provide syntax specifically designed for constructor-based object creation and inheritance. A class's instance methods are normally placed on the class prototype rather than copied onto each instance. Underneath the class syntax, JavaScript still uses objects and prototype-based property lookup. Therefore, understanding constructor functions and prototypes helps explain class behavior.