深拷贝方法

function deepClone(source) {
    if (source === null || typeof source !== 'object')
        return source;
    const target = source.constructor === Array ? [] : {}
    for (let key in source) {
        // 忽略从原型链上继承的属性
        if (source.hasOwnProperty(key)) {
            if (source[key] && typeof source[key] === 'object')
                target[key] = deepClone(source[key])
            else
                target[key] = source[key]
        }
    }
    return target
}
posted @ 2022-03-08 08:37  Bin_x  阅读(22)  评论(0)    收藏  举报