Javascript 数组拷贝

Javascript 数组深拷贝和浅拷贝

//数组浅拷贝

-> var oldArray = [0,1,2];

-> var newArray = oldArray.slice(0);

-> oldArray.pop();

<- [0,1]

-> newArray

<- [0,1,2]


//数组深拷贝

-> var oldArray = [{a:1},{b:2}];

-> var newArray = JSON.parse(JSON.stringify( oldArray ));

-> oldArray.pop();

<- [{a:1}]


-> newArray

<- [{a:1},{b:2}]