实现js对象的深拷贝方法deepClone ?

function deepClone(data){
    let type = Object.prototype.toString.call(data);
    let res;

    if ( type === '[object Array]'){
         res = [];
    } else if ( type === '[object Object]'){
         res = {};
    } else {
         return;
    }

    if ( type === '[object Array]' ){
         for ( let i = 0; i < data.length; i++ ){
               res.push(deepClone(data[i]))
         }
    } else {
         for ( i in data ){
               res[i] = deepClone(data[i]);
         }
    }

    return res;
}

你可能感兴趣的:(实现js对象的深拷贝方法deepClone ?)