{...object}
The first time I looked at this I was really confused about what it was supposed to mean, or even do, but harnessing the power of the ...
For further actions, you may consider blocking this person and/or reporting abuse
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="otherName
And 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}
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 :)
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