# cloneDeep

# Description

这个方法类似 clone ,除了它会递归拷贝 value。(注:也叫深拷贝)

# Params

value

# Return

{*} -- 拷贝的结果

# Depend

import baseClone from './.internal/baseClone.js'

baseClone 源码分析

# Code

const CLONE_DEEP_FLAG = 1
const CLONE_SYMBOLS_FLAG = 4

function cloneDeep(value) {
  return baseClone(value, CLONE_DEEP_FLAG | CLONE_SYMBOLS_FLAG)
}

# Analyze

调用 baseClone 进行拷贝,并返回结果,这里传入的 bitmask 为 4 | 1, 表示深拷贝和拷贝 symbol

4 | 1 // 0100 | 0001 => 5

# Remark

  1. | (按位或) MDN (opens new window)

# Example

const objects = [{ 'a': 1 }, { 'b': 2 }]

const deep = cloneDeep(objects)
console.log(deep[0] === objects[0]) // => false