Skip to content

Instantly share code, notes, and snippets.

@DefectingCat
Last active April 22, 2022 08:57
Show Gist options
  • Save DefectingCat/b9f86b3a52b1294f25a9e01e9fcfa600 to your computer and use it in GitHub Desktop.
Save DefectingCat/b9f86b3a52b1294f25a9e01e9fcfa600 to your computer and use it in GitHub Desktop.
let obj = {
a: "xfy",
b: {
c: "test",
},
d: 123,
e: [1, 2, 3, 4, 5],
f: {
g: 'array',
h: [1, 2, 3, 4, 5, 6, {
i: '嘤嘤嘤'
}]
}
};
let test = {};
function deepCopy(origin, target) {
target = target || {};
let sw = {
Object: "[object Object]",
Array: "[object Array]",
Function: "[object Function]",
};
for (let i in origin) {
let detect = Object.prototype.toString.call(origin[i]);
if (detect === sw.Object) {
target[i] = {};
deepCopy(origin[i], target[i]);
} else if (detect === sw.Array) {
target[i] = [];
deepCopy(origin[i], target[i]);
} else if (detect === sw.Function) {
target[i] = function() {};
deepCopy(origin[i], target[i]);
} else {
target[i] = origin[i];
}
}
}
deepCopy(obj, test);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment