es5数组复制
var arr=[1,2,3];
var arr2=[];
for(var i=0; i<arr.length; i++){
arr2[i]=arr[i];
}
arr2.pop();
console.log(arr, arr2);
es6数组复制
var arr=[1,2,3];
var arr2=Array.from(arr);
arr2.pop();
console.log(arr, arr2);
//简写
var arr=[1,2,3];
var arr2=[...arr];
arr2.pop();
console.log(arr, arr2);
网友评论