var arr=[1,2,3,4,5,6];
function fun1(arr){
var arr1=[];
for(var i=0;i<arr.length;i++){
arr1.push(arr[i]);
}
return arr1;
}
var res = fun1(arr);
arr.push[0,9,8];
console.log(res);
對象深拷貝
var obj={name:"張三",age:43}
function fun2(obj){
var obj1={};
for(x in obj){
obj1[x]=obj[x];
}
return obj1;
}
var res = fun2(obj);
obj.name="李四";
console.log(res);
結(jié)合到一個(gè)函數(shù)里
function fun(arr){
if(arr.constructor.name==="Array"){
var arr1=[];
for(var i=0;i<arr.length;i++){
arr1.push(arr[i]);
}
return arr1;
}else{
var obj={}:
for(x in arr){
obj[x]=arr[x];
}
return obj;
}
}