简体   繁体   English

为什么array.push()似乎无限次推?

[英]Why does array.push() seem to push infinite times?

Consider the following javascript code, 考虑以下javascript代码,

>> a = [1, 2]
  Array [ 1, 2 ]

>> a.push(a) 
  Array [ 1, 2, Array[3] ] 

On expanding the array, a , we get an array with infinite depth. 在扩展数组a ,我们得到一个具有无限深度的数组。 (as viewed on the firefox developer console) (在firefox开发者控制台上查看)

Array[3]
| 0: 1
| 1: 2
  2: Array[3]
  | 0: 1
  | 1: 2
  | 2: Array[3]
    | 0: 1
    | 1: 2
    | 2: Array[3]
    ....

[1] Why is a getting appended more than once? [1]为什么不止一次附加?
[2] At what depth does it stop? [2]它停在什么深度?

It is just pushed once. 它被推了一次。 While the array is referencing itself ( circular refernce ), you see more than one inserted arrays. 当数组引用自身( 循环引用)时,您会看到多个插入的数组。 The display is dependent of the browser. 显示取决于浏览器。

If you try to get a JSON string, then JSON.stringify breaks with an error 如果您尝试获取JSON字符串,则JSON.stringify会因错误JSON.stringify中断

 Circular reference in value argument not supported 

 var a = [1, 2] console.log(a); a.push(a); console.log(a); JSON.stringify(a); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

It's not getting pushed an infinite number of times. 它没有被推动无数次。 It's a matter of how Firefox Developer Console presents the element. 这是Firefox Developer Console如何呈现元素的问题。 Expanding the array and clicking on it again, under itself, will always show you how the array looks like. 扩展数组并再次单击它本身,将始终显示数组的外观。 So you can expand it and click it an infinite number of times, but still, it's there only once . 所以你可以扩展它并无限次点击它,但是,它只有一次

You've created a circular reference. 您已经创建了循环引用。

When you push the array onto itself, the last element in the array becomes a reference to the entire array -- including that last element. 当您将数组推送到自身时,数组中的最后一个元素将成为对整个数组的引用 - 包括最后一个元素。 So the last element in that last element is also a reference to the entire array, and so on. 因此,最后一个元素中的最后一个元素也是对整个数组的引用,依此类推。

It's infinite in the same sense that a circle is infinite, and "appends more than once" only in the same sense that if you go around a circle more than once you'll hit the same spot more than once. 它是无限的,就像一个圆圈是无限的,并且“不止一次地追加”只是在同一意义上,如果你不止一次绕过一个圆圈,你会不止一次地击中同一个地方。

Because the javascript gives the reference to the 'a' variable, and not the actual object, so if you push the a to itself, you push the memory address what hold the object, and it creates a circular reference. 因为javascript提供了对'a'变量的引用,而不是实际的对象,所以如果你将a推到自身,你可以推送保存对象的内存地址,并创建一个循环引用。

[1, 2, reference to this object ] [1,2, 参考此对象 ]

It is actually pushing it only one time. 它实际上只推动了一次。 It doesn't push the array elements, but the address of the array. 它不会推送数组元素,而是推送数组的地址。 Firefox console just tries to show what's in there, but ends up showing the same thing infinitely. Firefox控制台只是试图显示其中的内容,但最终会无限地显示相同的内容。

Not just Firefox, Chrome also does that. 不仅仅是Firefox,Chrome也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM