Introduction to the assignment of JS arrays
- 2020-03-30 02:18:05
- OfStack
var test=[1,2,3,4,5,6,7];
var arr=test;
arr.splice(2,1);
alert(test);//1,2,4,5,6,7
JS arrays are essentially objects. Therefore, the above source code ends up printing 1,2,3,4,5,6. This is because assigning a test to arr is actually assigning a reference to an array to arr, so the operation arr will do the same
Change the source array.
To implement array cloning, you can:
Array.prototype.clone=function(){
return this.slice(0);
}