Home  >  Article  >  Web Front-end  >  Introduction to JS array assignment_javascript skills

Introduction to JS array assignment_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:56:231166browse

Copy code The code is as follows:

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 finally prints 1,2,3,4,5,6. This is because assigning test to arr actually assigns the reference of the array to arr, so operating arr will also

Change the source array.

To implement array cloning, the following methods can be used:

Copy the code The code is as follows:

Array.prototype.clone=function(){
return this.slice(0);
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn