September 2018
Intermediate to advanced
302 pages
7h 17m
English
Although using the singleton pattern is discouraged nowadays, it is very beneficial to learn how to create this mechanism. For this code example, we will use ECMAScript 6 classes and ECMAScript 7 static fields:
export default class Singleton { static instance; constructor() { if (Singleton.instance) { return Singleton.instance; } this.instance = this; }}
We are changing the behavior of the constructor. First, before returning anything, we need to check if the instance has already been created. If so, the current call returns that instance instead.
Read now
Unlock full access