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.
What new does
Section titled “What new does”When a function is called with new, JavaScript does four things:
- It creates a new empty object.
- It sets that object’s prototype to the function’s
prototypeproperty, so the object can use the methods stored there. - It runs the function with
thisset to the new object, so the function can add properties to it. - 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: donetruetrueThe 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.
Forgetting new
Section titled “Forgetting new”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:
undefinedWater the plantsThis 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')Returning a value from a constructor
Section titled “Returning a value from a constructor”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 }Classes
Section titled “Classes”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.