TIL: Safe Cloning in JS
While working more on my React lessons tonight, I learned a helpful hint for ensuring that a new copy of an object gets created rather than just creating a new reference to the original object.
The lesson focused on the newer ES6 way of dealing with this using the new
Let’s assume that you have an array like so:
let fruits = ['apple','banana','grape']
If you wanted to make a copy of that array, you could use the spread method like so:
This will create a brand new array from the original. Any changes then made to the duplicate will not update the original.
This will take all of the properties of the
person object and add them into the new