JavaScript array assign issue
This question already has an answer here:
When you do something like array2 = array1;
you are just setting array2
as a reference to array1
. To make a copy of array1 you need to do array2 = array1.slice();
Furthermore, you can not set Array elements with array1.value1 ='1';
. What you have done there is convert your array to an Object. So what you really should be doing is:
var array1 = [];
var array2 = [];
array1[0] = 1;
array2 = array1.slice();
array2[1] = 2;
By doing array2 = array1;
you are assigning the array1 object to array2 variable. So modifying array2 will modify the object associated ie array1
Because you pass by reference array1
to array2
. You need to do a copy like:
array2 = new Array(array1);链接地址: http://www.djcxy.com/p/20990.html
上一篇: 如何将范围模型的引用传递给函数
下一篇: JavaScript数组分配问题