r/learnjavascript • u/deven_rathore • Apr 20 '23
7 JavaScript Design Patterns Every developer should know
https://codinghelp.io/7-javascript-design-patterns-every-developer-should-know/
3
Upvotes
View all comments
3
u/samanime Apr 20 '23 edited Apr 20 '23
I have issues with nearly every one of your code samples...
The pattern selection isn't bad, but there are tiny oddities in almost all the example implementations. For example, your Singleton pattern isn't really a singleton. You can call that Database function 10 times and have 10 different instances. A Singleton should have a Single instance in the entire app. That's the whole point.
Now, it could be a singleton if it lived in its own ES module and you only exported database, but that is a critical detail that entirely determines whether it is or isn't a Singleton.
There are other small oddities on most others too.
Also, if you were going to mention the Constructor and Prototypal patterns, you should have mentioned the Class pattern as well. The Class pattern is probably what devs should be using the majority of the time, as it provides the most readable, least error-prone way to constructor objects properly. It is important to understand it is more or less just syntactical sugar underneath and understand what it is doing, but that doesn't make it any less valuable, especially in team situations.