Good article! However, I feel like it's important to mention that the spread operator does not perform a deep clone on objects, but rather a shallow copy. This means that the spread operator will copy simple values (such as integers, strings and such) but keep the same references for objects. For example:
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
Hey Fran, thanks for the addendum. I actually should have probably put a little more clarity on this part, but i've made a small correction and your example is also super clear
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
I love spread operator it has saved me lot of times.. instead of using Array.prototype.slice..
Because I always mix it up with Array.prototype.splice...
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
I have read so many articles on the same topics
But you are special. I know i should be writing the code as well at the same time
But here i go looking to read another article from you
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
Hey Birju, you're right. I try to keep these as simple as possible and sometimes overlook some of the more advanced concepts in favor of clarity. Will make a note, thanks
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
Full stack application developer. Life-long learner. Pragmatic programmer. Believer in clean coding. Proponent for extensible and reusable code. Jazz music lover.
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
Front End Developer working to connect patients, caregivers, and loved ones @ Inspire. Always looking for ways to improve the world around me and the systems in place.
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
Good article! However, I feel like it's important to mention that the spread operator does not perform a deep clone on objects, but rather a shallow copy. This means that the spread operator will copy simple values (such as integers, strings and such) but keep the same references for objects. For example:
This prints 2 and 2 because the property b was shallow copied, which makes copy.b point to the exact same object as obj.b.
Hey Fran, thanks for the addendum. I actually should have probably put a little more clarity on this part, but i've made a small correction and your example is also super clear
Great intro to spread! Looks super useful for making copies. Though at the end, I believe you mean to say
original.child == copy.child(not
original.child == original.copy) is that right?
🤦♀️This is why you dont hotfix posts while doing 3 other things. Thanks! :)
Hi Marina,
thanks a lot for your instructive and funny to read article. Like your style.
I would like to ask you (if you ever revisit this article) , about what you say
baseObject.name="otherNameAnd when I consolelog both, the copy remain untouched.
Thanks in advance if you ever take the time to answer!
I love spread operator it has saved me lot of times.. instead of using Array.prototype.slice..
Because I always mix it up with Array.prototype.splice...
All ways lead to Rome :) But this one is definitely one of the easier/shorter ones
Yeah
Once again thankyou
I have read so many articles on the same topics
But you are special. I know i should be writing the code as well at the same time
But here i go looking to read another article from you
Does it seems like in Python? that
func(1, 2, a=1, b=2)
def func(args, **kwargs):
print(*args) --> [1, 2]
print(*kwargs) --> {a: 1, b: 2}
Nice article. Would you mind mentioning that the copy with the spread operator creates a shallow copy of the object and not a deep one.
Hey Birju, you're right. I try to keep these as simple as possible and sometimes overlook some of the more advanced concepts in favor of clarity. Will make a note, thanks
Great article, very clear!
Hey Gabriel, thanks a ton :D
Really nice and easy way to make copy ! :)
Thanks Drozerah!
I always preferred the term 'splat' operator. It sounds so much cooler. Nice article, thanks!
Thanks for reading :)
Really great explanation of this! Especially like that you gave the example of copying an object in that way. That's a super important use case.
You're right, it's very simple and super useful :)