Skip to content

Instantly share code, notes, and snippets.

@jpenney1
Created April 16, 2018 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpenney1/1f4638eff6ffb8e0ea81c6f9a6c6bf49 to your computer and use it in GitHub Desktop.
Save jpenney1/1f4638eff6ffb8e0ea81c6f9a6c6bf49 to your computer and use it in GitHub Desktop.
Checkout out how the deep clone changes does not change the reference to the object in numsRef[4]. Uncomment line 16, and see how the assignment copy assigns even references, allowing for the original objects to be mutated through the 'copies'.
let numsRef = {
1: 1,
2: 2,
3: 4,
4: { 1: 'hi' }
};
let deepObj = {
numbers: numsRef
};
let newObjectDeepClone = JSON.parse(JSON.stringify(deepObj));
newObjectDeepClone.numbers[4] = 'original unchanged';
let newObjectAssign = Object.assign({}, deepObj);
// newObjectAssign.numbers[4] = 'original changed';
console.log('original: ', deepObj);
console.log('newObjectAssign: ', newObjectAssign);
console.log('newObjectDeepClone: ', newObjectDeepClone);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment