|
| 1 | +/** |
| 2 | + * 深拷贝 |
| 3 | + * 类型 |
| 4 | + * 1. 简单数据类型(number、string、boolean、null、undefined..) |
| 5 | + * 2. 对象(object、正则、日期等) |
| 6 | + * 3. 函数 |
| 7 | + * 4. 数组 |
| 8 | + */ |
| 9 | + |
| 10 | +function deepClone(originObj) { |
| 11 | + const result = {} |
| 12 | + function helpFunction(obj, res) { |
| 13 | + for (const key in obj) { |
| 14 | + if (Array.isArray(obj[key])) { |
| 15 | + obj[key] = obj[key] |
| 16 | + .toString() |
| 17 | + .split(',') |
| 18 | + .map((item) => Number(item)) |
| 19 | + } else if (obj[key] instanceof Object) { |
| 20 | + // 对象 |
| 21 | + res[key] = {} |
| 22 | + helpFunction(obj[key], res[key]) |
| 23 | + } else { |
| 24 | + res[key] = obj[key] |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + helpFunction(originObj, result) |
| 29 | + return result |
| 30 | +} |
| 31 | + |
| 32 | +const o = { |
| 33 | + name: 'coderwei', |
| 34 | + hello: [1, 2, 3, 4, 5], |
| 35 | + age: { |
| 36 | + s: 19, |
| 37 | + t: 9, |
| 38 | + g: { |
| 39 | + ts: 1, |
| 40 | + jks: 90, |
| 41 | + }, |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | +const newObj = deepClone(o) |
| 46 | +o.age.s = 2999 |
| 47 | +o.age.g.ts = 999 |
| 48 | +console.log(o) |
| 49 | +console.log(newObj) |
| 50 | + |
| 51 | +// chatgpt 的 demo |
| 52 | +function _deepClone(obj, cache = new WeakMap()) { |
| 53 | + // 如果obj不是一个对象或者是null,则直接返回 |
| 54 | + if (!isObject(obj) || obj === null) { |
| 55 | + return obj |
| 56 | + } |
| 57 | + |
| 58 | + // 如果obj已经被拷贝过了,则直接返回之前的拷贝结果,避免循环引用导致的死循环 |
| 59 | + if (cache.has(obj)) { |
| 60 | + return cache.get(obj) |
| 61 | + } |
| 62 | + |
| 63 | + let result |
| 64 | + |
| 65 | + // 处理Map对象 |
| 66 | + if (obj instanceof Map) { |
| 67 | + result = new Map() |
| 68 | + obj.forEach((value, key) => { |
| 69 | + result.set(_deepClone(key, cache), _deepClone(value, cache)) |
| 70 | + }) |
| 71 | + return result |
| 72 | + } |
| 73 | + |
| 74 | + // 处理Set对象 |
| 75 | + if (obj instanceof Set) { |
| 76 | + result = new Set() |
| 77 | + obj.forEach((value) => { |
| 78 | + result.add(_deepClone(value, cache)) |
| 79 | + }) |
| 80 | + return result |
| 81 | + } |
| 82 | + |
| 83 | + // 处理Date对象 |
| 84 | + if (obj instanceof Date) { |
| 85 | + result = new Date(obj.getTime()) |
| 86 | + return result |
| 87 | + } |
| 88 | + |
| 89 | + // 处理RegExp对象 |
| 90 | + if (obj instanceof RegExp) { |
| 91 | + result = new RegExp(obj.source, obj.flags) |
| 92 | + return result |
| 93 | + } |
| 94 | + |
| 95 | + // 处理函数 |
| 96 | + if (typeof obj === 'function') { |
| 97 | + const funcString = obj.toString() |
| 98 | + result = eval(`(${funcString})`) |
| 99 | + return result |
| 100 | + } |
| 101 | + |
| 102 | + // 处理普通对象和数组 |
| 103 | + result = Array.isArray(obj) ? [] : {} |
| 104 | + cache.set(obj, result) // 缓存当前对象,避免循环引用导致的死循环 |
| 105 | + for (let key in obj) { |
| 106 | + if (Object.prototype.hasOwnProperty.call(obj, key)) { |
| 107 | + result[key] = _deepClone(obj[key], cache) |
| 108 | + } |
| 109 | + } |
| 110 | + return result |
| 111 | +} |
| 112 | + |
| 113 | +function isObject(obj) { |
| 114 | + return typeof obj === 'object' && obj !== null |
| 115 | +} |
0 commit comments