deepClone


function deepClone(target, seen = new Set()) {
  if (!target || typeof target !== 'object') return target
  if (typeof target === 'function') return target
  if (['RegExp', 'Date', 'Map', 'Set'].includes(target.constructor.name)) {
    return new target.constructor(target)
  }

  if (seen.has(target)) return target
  seen.add(target)

  const result = Array.isArray(target) ? [] : {}
  for (const prop in target) {
    if (target.hasOwnProperty(prop)) {
      result[prop] = deepClone(target[prop], seen)
    }
  }
  return result
}

为什么不对函数进行深拷贝?对函数深拷贝没有意义。

你可能感兴趣的:(deepClone)