November 30, 2018
3 min read
The new
keyword does a few things behind the scenes to create a new instance of an object.
One of these things is to invoke the constructor of the object being used as the source of the new
. The constructor is a function responsible for building a new object to the specifications desired by its developer.
When the constructor is actually called, it is bound with the context of the new object instance being created. This means that this
inside of the constructor function refers to the new instance not the original object or class. By referencing this
you can format the new object instance as desired.
class Standard { constructor() { this.property = 'initial_value'; }}
Another more uncommon feature of the constructor function is the usage of a return
statement.
This allows you to override the normal object creation process and return a different object as a result of the new
expression.
Among other things, you can use this to create a Singleton.
let instance;
class Singleton { constructor() { if (instance) { return instance; } instance = new Object(); }}
const a = new Singleton();const b = new Singleton();a === b; // true