DEV Community

Discussion on: Singleton Pattern in JavaScript

Collapse
 
adgower profile image
Alex Gower

Can you explain this to a 5 year old?

Collapse
 
hi_artem profile image
Artem

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).

Collapse
 
adgower profile image
Alex Gower

Thank you,
From a 6 year old.