Last active
December 14, 2016 16:58
-
-
Save Magellol/1a3fdd042cd3c21e59ba9b6e1fea61c3 to your computer and use it in GitHub Desktop.
Array.concat() or the spread syntax creates shallow copies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const original = [ | |
[0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0] | |
]; | |
const clone = [...original]; | |
console.log(original === clone); // false. | |
console.log(original[0] === clone[0]); // true. | |
original.splice(0, 2); // clone would not be altered. | |
original[0].splice(0, 2, 'a'); // clone _will_ be altered because sub-arrays are passed in as reference. | |
// Same scenario with Array.concat(). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment