Created
April 19, 2017 13:27
-
-
Save jasonhodges/82be015cd49303f20b74f820acba8ca2 to your computer and use it in GitHub Desktop.
demo TypeScript deepcopy function
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
// https://egghead.io/lessons/typescript-deep-copy-aka-clone-objects-using-typescript | |
function deepcopy<T>(o: T): T { | |
return JSON.parse(JSON.stringify(o)); | |
} | |
const foo = { | |
x: { | |
y: { | |
z: 123 | |
} | |
} | |
}; | |
const bar = deepcopy(foo); | |
bar.x.y.z = 456; | |
console.log(foo); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
What about getters if the object has some getter what would you like to copy?
After used the JSON.parse(JSON.stringify(o) the getters will be lost.
What do you think?
Thanks