DEV Community


Discussion on: Singleton Pattern in JavaScript

aakatev profile image
Artem Author

If the 5 year old already has some experience with programming, then it may work:
One way to implement singleton pattern in JS, is to encapsulate (enclose) desired members into outer function, but provide reference to desired properties of these members using inner function (same scope as the members). We can then immediately execute (invoke) inner function, and assign the value to a variable in a global scope (or different scope depending on use).

adgower profile image
Alex Gower

Thank you,
From a 6 year old.