Skip to content

Using the new( ) Operator on Constructor Functions

The new operator calls a function as a constructor, which means it uses the function to build and return a new object. Before classes arrived in ES2015, this was the standard way to make many objects of the same shape, and classes still work the same way underneath.

When a function is called with new, JavaScript does four things:

  1. It creates a new empty object.
  2. It sets that object’s prototype to the function’s prototype property, so the object can use the methods stored there.
  3. It runs the function with this set to the new object, so the function can add properties to it.
  4. It returns the new object, unless the function itself returns a different object.

By convention a constructor’s name starts with a capital letter, which tells the reader it is meant to be called with new:

function Task(title, due) {
this.title = title;
this.due = due;
this.done = false;
}
Task.prototype.complete = function () {
this.done = true;
return `${this.title}: done`;
};
const chore = new Task('Water the plants', 'Friday');
console.log(chore);
console.log(chore.complete());
console.log(Object.getPrototypeOf(chore) === Task.prototype);
console.log(chore instanceof Task);

This prints:

Task { title: 'Water the plants', due: 'Friday', done: false }
Water the plants: done
true
true

The method complete is defined once on Task.prototype and shared by every task, rather than copied into each object. Prototypes explains how the lookup works.

Without new, a constructor is just an ordinary function call. It returns undefined, because it has no return statement, and this is no longer a new object. In sloppy mode this is the global object, so the properties land there instead:

function Task(title) {
this.title = title;
}
const oops = Task('Water the plants');
console.log(oops);
console.log(globalThis.title);

This prints:

undefined
Water the plants

This example runs as a sloppy-mode script, and a browser’s classic script behaves the same way, setting window.title. In strict mode, which modules and classes always use, this is undefined in a plain call, so the mistake fails loudly instead:

'use strict';
function Task(title) {
this.title = title;
}
try {
Task('Water the plants');
} catch (err) {
console.log(err.message);
}

This prints:

Cannot set properties of undefined (setting 'title')

A constructor normally has no return statement. If it returns a primitive, such as a number, the return value is ignored and new still gives back the new object. If it returns an object, that object replaces the one new created:

function Plain(name) {
this.name = name;
return 42;
}
function Replaced(name) {
this.name = name;
return { replacement: true };
}
console.log(new Plain('a'));
console.log(new Replaced('b'));

This prints:

Plain { name: 'a' }
{ replacement: true }

A class is the modern way to write a constructor. new does the same four steps, but a class cannot be called without new, so the mistake shown earlier cannot happen:

class Task {
constructor(title) {
this.title = title;
}
}
try {
Task('Water the plants');
} catch (err) {
console.log(err.message);
}

This prints:

Class constructor Task cannot be invoked without 'new'

Introduction to Classes covers the class syntax.

Steve Griffith covers this topic in his video Using the new Keyword in JavaScript.