0

我正在学习 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/

4

2 回答 2

2

如果您在调用shoplist(). 因为 javascript 通过引用传递数组,并且只有一个引用进入shopitems数组,所以当您在将 ids 数组传递给第二次调用之前修改 ids 数组时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数组的末尾。因此,每次调用 shoplist 时,您都会在 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)

在此代码示例中, list 将包含两个元素,每个元素将指向x其 contains的同一个实时版本[1,2]

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

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

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

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

var x = [];
var list = [];
x.push(1);       // contains contains [1]
list.push(x);    // list is [[1]]
x = x.slice(0);  // make a copy of x, 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)

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

如果您是副本,则必须显式制作新数组或显式制作副本。

于 2012-08-21T19:30:54.197 回答
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

在我看来它工作得很好。

于 2012-08-21T19:34:23.510 回答