Created
April 16, 2018 05:05
-
-
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'.
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
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