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.
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
class
syntax sugar and the use ofclosures
to simulate OOP, I think it is better to go for theclass
one to attach to one standard and keep it clean instead of keeping modifying theprototype
.Keep functions as simple as possible
void
functions)function composition
Always 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/catch
every 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.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!
Great article, just one thing. Isn't the first snippet supposed to be
addChild(name)
?Thank you! The intention of addChild's
frog
argument was that if the frog were to have children then it should have be instantiated withcreateFrog
right before being passed in the instance. So there's the parent frog who had a baby (and the baby frog should have been instantiated withcreateFrog
which 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!
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
break
which unnecessarily adds more lines than needed. In those situations it looks much nicer to just write if/else conditions while keeping the code short.I didn't know about (2). I'll definitely keep that one in mind. Thanks for sharing!