Created
April 18, 2018 23:44
-
-
Save HuangXiZhou/0195d1348bd1fe0f2a8db69b4e0b07f8 to your computer and use it in GitHub Desktop.
JavaScript DataCopy
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
// Extend copy | |
function extendCopy(o) { | |
let c = {}; | |
for (let i in o) { | |
c[i] = o[i]; | |
} | |
c.uber = o; | |
return c; | |
} | |
// Deep copy | |
function deepCopy(o) { | |
let c = arguments[1] || {}; | |
for (let i in o) { | |
if (typeof o[i] === 'object') { | |
c[i] = o[i] instanceof Array | |
? [] | |
: {}; | |
deepCopy(o[i], c[i]) | |
} else { | |
c[i] = o[i]; | |
} | |
} | |
return c; | |
} | |
// Easy deep copy(Can't handle function) | |
function deepCopyEasy(o) { | |
return typeof o === 'object' | |
? JSON.parse(JSON.stringify(o)) | |
: o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment