TS - Private constructor.

by Rodwyn Moreno

TypeScript

class Mutant { 
  static instance: Mutant;

  private constructor(public name: string) { }

  static createMutant() { 
    if (!Mutant.instance) { 
      Mutant.instance = new Mutant("Apocalypse");
    }

    return Mutant.instance;
  }
}

let mutant = Mutant.createMutant();
console.log(mutant);