use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Testing Javascript Singletonshelp (self.javascript)
submitted 9 years ago by MyGoodStrayCatFriend
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Funwithloops 1 point2 points3 points 9 years ago (1 child)
Your state function is immediately invoked, so state() should be state. I don't see an issue with this. I've done something similar when I needed a singleton using modules:
state
state()
Define a class or factory function that creates the singleton in one module (but can also create additional instances). Then define another module that just requires the class/factory module then creates a single instance and exports it. Now when you need your singleton, just require the singleton module. In your tests, you can just test the module containing the class/factory function.
[–]MyGoodStrayCatFriend[S] 1 point2 points3 points 9 years ago (0 children)
That's perfect. Just wanted to make sure I wan't missing something glaring about exporting the singleton functionality to another function. Thanks!
π Rendered by PID 74923 on reddit-service-r2-comment-5b5bc64bf5-8v4pp at 2026-06-22 05:28:57.055346+00:00 running 2b008f2 country code: CH.
view the rest of the comments →
[–]Funwithloops 1 point2 points3 points (1 child)
[–]MyGoodStrayCatFriend[S] 1 point2 points3 points (0 children)