javascript深拷贝

function deepClone(obj) {
    if (obj === null || typeof obj !== 'object') {
        return obj;
    }

    if (obj instanceof Date) {
        return new Date(obj);
    }

    if (obj instanceof Array) {
        const newArr = [];
        for (let i = 0; i < obj.length; i++) {
            newArr[i] = deepClone(obj[i]);
        }
        return newArr;
    }

    if (obj instanceof Object) {
        const newObj = {};
        for (let key in obj) {
            if (obj.hasOwnProperty(key)) {
                newObj[key] = deepClone(obj[key]);
            }
        }
        return newObj;
    }
}

你可能感兴趣的:(JavaScript,javascript,前端,开发语言)