js数组和对象的深复制方法

参考ivew源码

function deepCopy(data) {
     
    const t = typeOf(data);
    let o;

    if (t === 'array') {
     
        o = [];
    } else if ( t === 'object') {
     
        o = {
     };
    } else {
     
        return data;
    }

    if (t === 'array') {
     
        for (let i = 0; i < data.length; i++) {
     
            o.push(deepCopy(data[i]));
        }
    } else if ( t === 'object') {
     
        for (let i in data) {
     
            o[i] = deepCopy(data[i]);
        }
    }
    return o;
}

你可能感兴趣的:(JavaScript)