Extract Functions Arguments using Destructure in JavaScript
ES6 Destructuring is terrific at extracting value from your arguments. So ...
For further actions, you may consider blocking this person and/or reporting abuse
Nice read!
Really well explained, I tend to use objects for this purpose, so the user sees the parameter names on the function call, but this is a great approach as well, even better if using
arguments.Thanks Manolo! And glad you found it helpful!
Totally, passing object is super cool! which kinda acts like named parameters where you don't have to worry about the order. I believe this is what you're referring to:
I have a tidbit on it as well > samanthaming.com/tidbits/18-named-... π
Yup that was exactly it :) I'll check it out
I have to agree with you in this sense, I guess the point of the post was to not use
arg[0]to access arguments and to show and explain how destructuring works. But in the example you show, it is kinda redundant. I think this will confuse more people than not.This is why I use objects instead, at least you give some useful feature to the user (named parameters).
This:
fn({ name: 'bob' }).Instead of:
fn('bob').For 2 arguments it is kinda not needed, although when passing in a bunch of arguments it is cleaner to do it this way.
If I have to pass a lot of parameters I tend to use with objects instead:
For sure, in this simple example, the rest+destructuring combo doesn't really make sense. I'd stick with the 2nd way for sure πI think the community example was just showing a different way of doing things that achieve the same thing. Coding is all about communication, so we should pick the one that best facilitates that. I always enjoy seeing all the different ways because it builds up my toolbox. The best tool is always dependent on the situation. Thanks for pointing this out and allowing me to adjust my code notes π
Thanks for the question Tomasz! I just re-wrote the article to add some explanation. Hopefully, it clarified some things...if you don't mind, can you have a read (the last section) and let me know if it helps π€If it doesn't, just let me know and I'll try again π
Learned something new! Thank you :)
Awesome, glad to hear that! thanks for reading π
Great technique!, Ad thanks the tip about default args @lukeshiru