Find me on medium
1. Use factory functions
If you don’t know what a factory function is, it’s simply a function (that isn’t a class or ...
For further actions, you may consider blocking this person and/or reporting abuse
I normally use a function with try/catch inside it so that I don't need to always add try/catch blocks.
These are actually good tips! I am a big fan of the factory functions myself and prefer them over classes. Good to see that one coming by for once. And I even learned something. I never knew why you would want to put functions on the
.prototype, but your explanation makes sense. Definitely gonna revise some of my packages with factory functions.I am so glad to hear that! I was hoping I would grab some readers who were stuck on that prototype mystery!
Hey! What good tips! Just a few comments about
Add methods on the .prototype when writing constructors
Even I'm not a huge fan of the
classsyntax sugar and the use ofclosuresto simulate OOP, I think it is better to go for theclassone to attach to one standard and keep it clean instead of keeping modifying theprototype.Keep functions as simple as possible
voidfunctions)function compositionAlways consider using try/catch when using JSON.parse or JSON.stringify
Here depends a lot of your app structure, It is better if you don't use
try/catchevery time you use it but let the error goes bubbled up until you're able to handle it properly and show a message to the user or print it to the logs.Great article, just one thing. Isn't the first snippet supposed to be
addChild(name)?Thank you! The intention of addChild's
frogargument was that if the frog were to have children then it should have be instantiated withcreateFrogright before being passed in the instance. So there's the parent frog who had a baby (and the baby frog should have been instantiated withcreateFrogwhich is now also an instance of a frog sometime in our app before being passed as a child)So in the code it would look something like this:
or
Great summary! I would like to think that I'm perfect in all of these 7 practices, but I really need to start writing tests. I'll be sure to check out the link you provided. Thanks!
Your welcome!
I didn't know about (2). I'll definitely keep that one in mind. Thanks for sharing!
Why type is not a switch I don't know, although I have never heard that this is a conversation. Nor that using prototype when a class keyword is more appropriate.
A switch here would be just fine since it just returns the result. However in a lot of situations when i'm considering switch cases I don't have to return the value evaluated from the case block which ultimately I'd become forced to put in a
breakwhich unnecessarily adds more lines than needed. In those situations it looks much nicer to just write if/else conditions while keeping the code short.