var arr1 = [1,2,3,4,5];
var arr2= arr1;
arr2.push(6);
alert(arr1); //1,2,3,4,5,6
alert(arr2);//1,2,3,4,5,6
其实arr1和arr2指向的是同一个地址,解决这个问题也很简单:
var arr1 = [1,2,3,4,5];
var arr2= [];
for(var i=0;i<arr1.length;i++){
arr2[i] = arr1[i]
}
arr2.push(6);
alert(arr1);//12345
alert(arr2);//123456