Skip to content

Commit

Permalink
deepclone 循环引用
Browse files Browse the repository at this point in the history
  • Loading branch information
mogu authored and mogu committed Jul 30, 2020
1 parent b9a6308 commit 39af837
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions 专题系列/deepclone之循环引用问题.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 39af837

Please sign in to comment.