Skip to content

Instantly share code, notes, and snippets.

@HuangXiZhou
Created April 18, 2018 23:44
Show Gist options
  • Save HuangXiZhou/0195d1348bd1fe0f2a8db69b4e0b07f8 to your computer and use it in GitHub Desktop.
Save HuangXiZhou/0195d1348bd1fe0f2a8db69b4e0b07f8 to your computer and use it in GitHub Desktop.
JavaScript DataCopy
// 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