There are many questions about singleton pattern in javascript. All answers i found recommend:
-
IIFE that produces a factory hidden in a closure. Then using that factory, we can call
getInstance()
-
to avoid relying on
require
(and node.js module) as a way of getting singleton.
but then we have to store that factory somewhere so it can be accessed by other modules. it can't be stored in any random module because then we rely on require
. in such case it seems like the only ways to have safe singleton is to:
- store the factory in global variable or
- let top level script manage creation of the singleton instance (and pass it to other modules)
is one of them better than another? are there any other ways?
Aucun commentaire:
Enregistrer un commentaire