From 39af837bfcdcd81937407ca4a2dc6f1dd7ae96ff Mon Sep 17 00:00:00 2001 From: mogu Date: Thu, 30 Jul 2020 17:30:58 +0800 Subject: [PATCH] =?UTF-8?q?deepclone=20=E5=BE=AA=E7=8E=AF=E5=BC=95?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\225\347\224\250\351\227\256\351\242\230.js" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 "\344\270\223\351\242\230\347\263\273\345\210\227/deepclone\344\271\213\345\276\252\347\216\257\345\274\225\347\224\250\351\227\256\351\242\230.js" diff --git "a/\344\270\223\351\242\230\347\263\273\345\210\227/deepclone\344\271\213\345\276\252\347\216\257\345\274\225\347\224\250\351\227\256\351\242\230.js" "b/\344\270\223\351\242\230\347\263\273\345\210\227/deepclone\344\271\213\345\276\252\347\216\257\345\274\225\347\224\250\351\227\256\351\242\230.js" new file mode 100644 index 0000000..6167edf --- /dev/null +++ "b/\344\270\223\351\242\230\347\263\273\345\210\227/deepclone\344\271\213\345\276\252\347\216\257\345\274\225\347\224\250\351\227\256\351\242\230.js" @@ -0,0 +1,16 @@ +function deepClone(obj, map = new Map()) { + if(!obj || typeof obj !== 'object') return obj + if(map.get(obj)) { + return map.get(obj) + } + let res = Array.isArray(obj) ? [] : {} + map.set(obj, res) + let keys = Object.keys(obj) + let key, temp + for(let i = 0, len = keys.length; i < len; i++) { + key = keys[i] + temp = obj[key] + res[key] = deepClone(temp, map) + } + return res +} \ No newline at end of file