Skip to content

Instantly share code, notes, and snippets.

@zburgermeiszter
Last active March 9, 2018 15:40
Show Gist options
  • Select an option

  • Save zburgermeiszter/2cbe02999ecb65a3062d4b58d74f39f7 to your computer and use it in GitHub Desktop.

Select an option

Save zburgermeiszter/2cbe02999ecb65a3062d4b58d74f39f7 to your computer and use it in GitHub Desktop.

Revisions

  1. zburgermeiszter revised this gist Mar 9, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions singleton.ts
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // https://stackoverflow.com/a/36978360
    class Singleton {
    private static _instance: Singleton;

  2. zburgermeiszter created this gist Mar 8, 2018.
    20 changes: 20 additions & 0 deletions singleton.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    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);