DEV Community

Volodymyr Yepishev
Volodymyr Yepishev

Posted on • Edited on

Create Singleton with ES2022

Create a singleton? Why? For the glory of OOP, of course!

ES2022 brings new cool feature of static initializers, which means now it's even easier to create
singletons in JS/TS.

  • make constructor private so scrubs consumers don't instantiate your class;
  • create a private static vip-one-of-a-kind instance;
  • initialize the vip-one-of-a-kind instance with new ES2022 bells and whistles static block;
  • serve the private static vip-one-of-a-kind instance using public static getter
class Foo {
    private static instance: Foo;

    static {
        Foo.instance = new Foo();
    }

    public static get getInstance(): Foo {
        return Foo.instance;
    }

    private constructor() { }
}
Enter fullscreen mode Exit fullscreen mode

Looks more neat to me than the old way:

class Foo {
    private static instance: Foo;

    public static get getInstance(): Foo {
        if(!Foo.instance){
            Foo.instance = new Foo();
        }
        return Foo.instance;
    }

    private constructor() { }
}
Enter fullscreen mode Exit fullscreen mode

Though they both are consumed in the same way

const bar = Foo.getInstance;
Enter fullscreen mode Exit fullscreen mode

Huzzah!

Top comments (0)