将数组传递给函数多次

时间:2012-08-21 19:27:38

标签: javascript

我正在学习javascript,我正在试图找出为什么我的下面的功能不能按预期工作。请参阅2个代码示例中的说明:

// First time I call the shoplist function I pass [1] in the argument. Results are as I expect:

var shopitems = [];

function shoplist(ids) {
    alert("ids passed to shoplist function: " + ids); // 1
    alert("current ids in shopitems var: " + shopitems); // (empty)
    shopitems.push(ids);
    alert("ids in shopitems after pushing: " + shopitems); // 1
    }


// Second time I call the shoplist functions I pass [1, 2] in the argument. Results are not what I would expect:

function shoplist(ids) {
    alert("ids passed to shoplist function: " + ids); // 1, 2
    alert("current ids in shopitems var: " + shopitems); // 1, 2  <--- Why is there 1, 2 and not only 1?
    shopitems.push(ids);
    alert("ids in shopitems after pushing: " + shopitems); // 1, 2, 1, 2

编辑:以下是完整代码(警告:可能非常令人困惑):http://dpaste.org/wXMy5/

2 个答案:

答案 0 :(得分:2)

如果在调用shoplist()的第二次调用之前修改SAME ID数组,则会出现问题。因为javascript通过引用传递数组,并且只有引用进入shopitems数组,所以在将id传递给shoplist()的第二次调用之前修改它时,你也无意中修改了shopitems[0]太。如果你调用它的第一次和第二次shoplist()的每个参数都是完全独立的数组,那么你就不会遇到这个问题,但如果第二次调用只是传递了对第一个数组的修改,那么你将拥有这个问题。

快速说明是:

// this will not have the problem because each call to shoplist
// is passing a completely separate array
var list = [1];
shoplist(list);
list = [1,2];      // create new array
shoplist(list);    // shoplist is [[1], [1,2]]

// this will have the problem because they are the same array
var list = [1];
shoplist(list);
list.push(2);     // modify first array
shoplist(list);   // shoplist is [[1,2], [1,2]] and both array elements are actually the same array

有关更详细的说明:.push(ids)ids的任何内容作为新项添加到shopitems数组的末尾。因此,每次打电话给商店列表时,都会在商店项目结束时获得一个新商品。但是,由于您要添加的项是一个数组,它会添加对该数组的引用,而不是该数组的副本。如果您随后更改了该数组,则shopitems数组条目将指向更改的数组版本。

您可以在此代码中看到:

var x = [];
var list = [];
x.push(1);       // contains contains [1]
list.push(x);    // list is [[1]]
x.push(2);       // x is [1,2]
list.push(x);    // list is [[1,2], [1,2]]  (contains two references to x)

在此代码示例中,列表将包含两个元素,每个元素都指向包含x的{​​{1}}的相同实时版本。

这是因为默认情况下,javascript传递数组和对象之类的引用。将数组元素推入容器数组时,它不会将该变量的静态副本放入数组中。它将指针放在原始变量上。如果您随后更改了原始变量,则该更改也会反映在数组中。

要将第二个条目与第一个条目分开,您需要有意识地复制第一个数组并将该副本推送到容器数组中,或者需要从头开始创建一个新数组并将其推入容器数组中。

例如,以下是在容器数组中创建两个独立元素的几种方法:

[1,2]

或者,复制var x = []; var list = []; x.push(1); // contains contains [1] list.push(x); // list is [[1]] x = []; // set x to a new array (the old version of x is still in list) x.push(1); // x is [1] x.push(2); // x is [1,2] list.push(x); // list is [[1], [1,2]] (contains two separate items)

x

这里要记住的重要一点是,在javascript中,对象分配或数组赋值不会复制。它只是指定一个指向原始数据结构的指针。如果您更改原始数据结构,则会反映在您所做的任何作业中。

如果您是副本,则必须明确制作新数组或明确复制。

答案 1 :(得分:1)

我运行了这段代码......

var shopitems = [];

shoplist([1]);
shoplist([1,2]);

function shoplist(ids) {
    WScript.Echo("ids passed to shoplist function: " + ids); // 1
    WScript.Echo("current ids in shopitems var: " + shopitems); // (empty)
    shopitems.push(ids);
    WScript.Echo("ids in shopitems after pushing: " + shopitems); // 1
    }

并得到了这个输出。

ids passed to shoplist function: 1
current ids in shopitems var:
ids in shopitems after pushing: 1
ids passed to shoplist function: 1,2
current ids in shopitems var: 1
ids in shopitems after pushing: 1,1,2

让我觉得它运作得很好。