SOURCE

// 1、JSON 方法
let obj = {
    a: 1,
    b: 2
}

let obj2 = JSON.parse(JSON.stringify(obj))
// 只能对对象格式数据深拷贝


// 2、
// 校验类型
let checkType = data => {
    return Object.prototype.toString.call(data).slice(8, -1);
}

let deepClone = target => {
    let targetType = checkType(target);
    let result;

    if (targetType === "Object") {
        result = {};
    } else if (targetType === "Array") {
        result = [];
    } else {
        return target;
    }

    for (let i in target) {
        let value = target[i];
        let valueType = checkType(value);

        result[i] = valueType === "Object" || valueType === "Array" ? deepClone(value) : value;
    }

    return result;
}

let arr1 = [1, 2, {name: 'test'}];
let arr2 = deepClone(arr1);
arr2[0] = 3;
arr2[2].name = "jack";
console.log(arr1)

let test_obj1 = {
    name: 'test',
    age: 12,
    job: {
        com: "xxxx",
        addr: "where"
    }
}
let test_obj2 = deepClone(test_obj1);
test_obj2.job.com = "noname";
test_obj2.age = 20;
console.log(test_obj1);
console.log(test_obj2);
console 命令行工具 X clear

                    
>
console