用递归的方式实现深拷贝

86 阅读1分钟

用递归的方式实现深拷贝

function _deepClone(source) { 
    let target; 
    if (typeof source === 'object') { 
        target = Array.isArray(source) ? [] : {} 
        for (let key in source) { 
            if (source.hasOwnProperty(key)) { 
                if (typeof source[key] !== 'object') { 
                    target[key] = source[key] 
                    } else { 
                    target[key] = _deepClone(source[key]) } 
                    } 
                } 
            } 
            else { 
            target = source 
            } 
            return target 
        }