路过的Javascript阵列功能通过值,使原数组不变 [英] Javascript passing arrays to functions by value, leaving original array unaltered
问题描述
我读过这里的许多答案,按价值和引用传递发送阵列JavaScript函数有关。但是我有发送阵列功能问题并保留原始数组不变。这个例子llustrates问题:
I've read many answers here relating to 'by value' and 'by reference' passing for sending arrays to javascript functions. I am however having a problem sending an array to a function and leaving the original array unaltered. This example llustrates the problem:
function myFunction(someArray)
{
// any function that makes an array based on a passed array;
// someArray has two dimensions;
// I've tried copying the passed array to a new array like this (I've also used 'someArray' directly in the code);
funcArray = new Array();
funcArray = someArray;
var i = 0;
for(i=0; i<funcArray.length; i++)
{
funcArray[i].reverse;
}
return funcArray;
}
我不明白为什么在这个功能有什么要改变原来的数组。
I can't understand why anything in this function should alter the original array.
调用如果函数调用被分配到一个新的数组此功能直接改变原始数组:
calling this function directly changes the original array if the function call is assigned to a new array:
myArray = [["A","B","C"],["D","E","F"],["G","H","I"]];
anotherArray = new Array();
anotherArray = myFunction(myArray);
// myArray gets modified!;
我试着用.valueOf()发送原始的:
I tried using .valueOf() to send the primitive:
anotherArray = myFunction(myArray.valueOf());
// myArray gets modified!;
我已经甚至试图通过子元素元素和子元素破阵列向下元件和分配所有的新的2-D阵列和原始阵列仍被修改
I have even tried breaking the array down element by element and sub-element by sub-element and assigning all to a new 2-d array and the original array still gets modified.
我也加入了子元素为一个字符串,处理它们,将它们分割回数组和原始数组仍然被修改。
I have also joined the sub-elements to a string, processed them, split them back into arrays and the original array still gets modified.
请,没有任何一个知道我可以通过数组值的函数,没有传递的数组改变?
Please, does any one know how I can pass the array values to a function and not have the passed array change?
推荐答案
在你的函数有这样的:
funcArray = new Array();
funcArray = someArray;
这实际上不会复制的someArray
而是引用它,这就是为什么原始数组被修改。
This won't actually copy someArray
but instead reference it, which is why the original array is modified.
您可以使用<一个href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/slice\"><$c$c>Array.slice()$c$c>创建数组的一个所谓的浅拷贝。
You can use Array.slice()
to create a so-called shallow copy of the array.
var funcArray = someArray.slice(0);
原来的阵列将是不变的,但它的每个元素仍然引用原始数组中的相应条目。对于深度克隆你需要递归做到这一点;最有效的方式是在以下问题进行讨论:
The original array will be unaltered, but each of its elements would still reference their corresponding entries in the original array. For "deep cloning" you need to do this recursively; the most efficient way is discussed in the following question:
<一个href=\"http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object\">What是克隆JavaScript对象?最有效的方式
的顺便说一句,我已经添加了 VAR
在 funcArray
。这样做使得局部的作用,而不是成为一个全局变量。的
Btw, I've added var
before funcArray
. Doing so makes it local to the function instead of being a global variable.
这篇关于路过的Javascript阵列功能通过值,使原数组不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!