js 数组,或者对象的浅拷贝和深拷贝

192 阅读1分钟

浅拷贝

function shandowCopy(arr){
    var judge =Object.prototype.toString.call(arr);
    var obj = judge == '[object Array]' ? []:{};
   for(i in arr){
      obj[i]= arr[i];
   }
   return obj
}

深拷贝:

function deepCopy(arr){
    <!--var judge =Object.prototype.toString.call(arr);-->
    return JSON.parse(JSON.stringify(arr))
}