var array1 = new Array("1","2","3"); var array2; array2 = array1; array1.length = 0; alert(array2); //returns empty
This approach is wrong because javascript is divided into primitive types and reference types (similar to java and c#). Array is a reference class type. array2 gets a reference, so modifications to array1 will affect array2. 2. Use slice() You can use slice() to copy, because slice() also returns an array.
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