Last active
April 22, 2022 08:57
-
-
Save DefectingCat/b9f86b3a52b1294f25a9e01e9fcfa600 to your computer and use it in GitHub Desktop.
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
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