5 comments

[ 2.7 ms ] story [ 9.8 ms ] thread
I have eventually learned to regret every singleton I've ever written.
Haha you wouldn't like Node then :)
It is a thing that bothers me a lot about Node. I much prefer the .NET style of namespaces over direct file importing, which I could have sworn C taught us was an antiquated way of doing things. They try to get around it with some goofiness of "partially loaded files" in the case of A-requires-B-requires-A, but it smells like a hack and I avoid it at all cost.

That said, it's not necessarily required. You can make any of your module files export only one function, a function that defines a class. This is the preferred pattern in most programming languages. I then use dependency injection to propagate my state around.

And that's generally the case in any programming language: if you're thinking of a singleton, dependency injection is the answer.

My next post is on class design in Node. Keep on the lookout, would love to get your feedback on it.
Nice post. I like it. Thank you.

BTW, do you write javascript to create apps on mobile?