Skip to content

Instantly share code, notes, and snippets.

@zburgermeiszter
Last active March 9, 2018 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zburgermeiszter/2cbe02999ecb65a3062d4b58d74f39f7 to your computer and use it in GitHub Desktop.
Save zburgermeiszter/2cbe02999ecb65a3062d4b58d74f39f7 to your computer and use it in GitHub Desktop.
Typescript singleton
// 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