Created
April 24, 2022 21:14
-
-
Save DoctorDerek/4b3f092b6fdc9ce3143b9bafaadba752 to your computer and use it in GitHub Desktop.
Not Safe to Copy: Date, undefined, and Infinity https://medium.com/p/c3f2783661e9
This file contains 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 notSafeToCopy = [new Date(), undefined, [Infinity]] | |
const copy = JSON.parse(JSON.stringify(notSafeToCopy)) | |
console.info(notSafeToCopy) // [Date object, undefined, [Infinity]] | |
console.info(copy) // ["2022-04-24T20:09:16.309Z", null, [null]] | |
// At least we did make a deep copy, but the data is all wrong: | |
const copyDos = JSON.parse(JSON.stringify(notSafeToCopy)) | |
copyDos[2][0] = -Infinity // Change deeply-nested [null] to [-Infinity] | |
console.info(notSafeToCopy) // [Date object, undefined, [Infinity]] | |
console.info(copyDos) // ["2022-04-24T20:09:16.309Z", null, [-Infinity]] | |
// The change to the "deep copy" didn't affect the original array. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment