Last active
March 9, 2018 15:40
-
-
Save zburgermeiszter/2cbe02999ecb65a3062d4b58d74f39f7 to your computer and use it in GitHub Desktop.
Typescript singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://stackoverflow.com/a/36978360 | |
class Singleton { | |
private static _instance: Singleton; | |
private constructor() { | |
console.log("Instantiated"); | |
} | |
public static getInstance(): Singleton { | |
console.log("getInstance()"); | |
return this._instance || (this._instance = new this()); | |
} | |
} | |
const i1 = Singleton.getInstance(); | |
const i2 = Singleton.getInstance(); | |
const i3 = Singleton.getInstance(); | |
console.log(i1 === i2); | |
console.log(i2 === i3); | |
console.log(i1 === i3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment