What is Destructuring?
Destructuring is a special really cool syntax feature in JavaScript, which lets us to extract values from arrays,...
For further actions, you may consider blocking this person and/or reporting abuse
I was a huge fan of destructuring as it gives me a more concise looking code. But when things get big and many objects are destructured, everything is just losing its context. Take this example
Not mentioning if you have a colliding property names then you have to make an alias for the colliding property name so you would still ended up in a messy code
Understood! I shall keep this in mind.
When using the
spreadsyntax to copy objects that havenested properties, it is crucial to understand how JavaScript handlesreferencesto thesenested properties. Thespread operatorcreates ashallow copyof the object. This means that while the top-level properties are copied, any nested objects or arrays are stillreferencedfrom the original object. Consequently, changes tonested propertiesin thecopied objectwill also reflect in theoriginal objectand vice versa.Let's take an example:
Here, we define an object
personwith properties likename,ageandaddress. Andaddressobject containscityandzip.Shallow Copy with Spread Operator
The spread operator
{ ...person }creates a shallow copy ofperson. This means that the top-level properties(name, age, and address)are copied directly intopersonCopy. However, sinceaddressis anobject(a nested property), only thereferenceto this object is copied, not theactual nested object itself.Modifying Nested Property
Since
personCopy.addressis areferenceto the sameaddressobject asperson.address, changingpersonCopy.address.cityto'Los Angeles'affects bothpersonandpersonCopy. Hence, bothconsole.logstatements output"Los Angeles".Modifying Nested Property Again
Similarly, when we change
person.address.cityback to'New York', it also affectspersonCopy.address.citybecause theyreferencethe sameaddressobject. As a result, bothconsole.logstatements output"New York".Massive! That's a brief write up! I learnt something new.
Thank you 😊!
Thank you for your detailed feedback! Pleased to know that you found the guide comprehensive and the examples clear. Looking forward to bringing you more useful content!
And hey!
Thanks again 🙃 for your support!
Really well-explained!!
Thanks a lot for your positive feedback! I’m always looking to improve.
Well explained... It was really helpful for me
Thank you! It’s great to hear that you've found the explanation helpful!
This is amazing, I should also consider writing.
Thank you so much! 😊
Writing is incredibly rewarding, and I'd love to see what you come up with. Let me know if you need any suggestions on getting started, don't hesitate to reach out!
I'd love to read what you write—go for it!
Great! Loved it
Great article!!
Would love to know the performance overhead of destructuring. As js devs, we don't really care abt minor performance issue, but still it would be interesting to know.
Thank you for your kind words. You asked so I did a quick research on this topic.
I didn't actually thought about the performance consideration before. Well, it is indeed a critical point to think about.
Well as I far as I realised, Destructuring in JavaScript does introduce a slight performance overhead compared to traditional property access (dot notation) due to the extra steps involved in creating and assigning variables. In scenarios where performance is critical, this overhead can become noticeable.
For instance:
There could be more examples. But I think these are enough.
But for most practical purposes, the performance difference is negligible and should not deter you from using destructuring.
As you have been developing applications in JavaScript long before I started, you might know all of this better than I do.
awesome :)
Thank u for the appreciation :)
All the lightbulbs went off above my head when you tied destructing and imports. Thank you!!!!
😊 🙌 😊 🙌