The article is nice, it gives some cool alternatives that not everyone might know about, a handful of which we use at work.
As others said, tho, some are harder to read.
A little side note about #4 and #5: it would be better to re-use the object and not to create it every time a function is called (eg: creating it inside array.map(), etc), it slows down your code a lot!
I'm saying this because it might not be obvious and also because I've had to refactor some code that was doing the exact same thing after I got hired ๐
A better approach would be to do something like this instead:
constcreateDogSwitcher=()=>{constobjectSwitch={border:'Border Collies are good boys and girls.',pitbull:'Pit Bulls are good boys and girls.',german:'German Shepherds are good boys and girls.',}constdefaultString='Im the default.'return(breed)=>objectSwitch[breed]||defaultString}constdogSwitch=createDogSwitcher()dogSwitch('border xxx')// Now use `dogSwitch` inside `map()`
If you want even better performance out of this, you can use a Map() instead of an object ๐๐ผ
thanks for comments! the idea was make an arrow function that return an object. I had thought about this option too, it is more semantic but much more code. It is in each developer as you feel more comfortable.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
The article is nice, it gives some cool alternatives that not everyone might know about, a handful of which we use at work.
As others said, tho, some are harder to read.
A little side note about #4 and #5: it would be better to re-use the object and not to create it every time a function is called (eg: creating it inside
array.map()
, etc), it slows down your code a lot!I'm saying this because it might not be obvious and also because I've had to refactor some code that was doing the exact same thing after I got hired ๐
A better approach would be to do something like this instead:
If you want even better performance out of this, you can use a Map() instead of an object ๐๐ผ
Edit: I think you have two #5s ๐ค
thanks for comments! the idea was make an arrow function that return an object. I had thought about this option too, it is more semantic but much more code. It is in each developer as you feel more comfortable.