[英]Is it possible for a JavaScript array to contain itself?
In Ruby, it's possible for an array to contain itself , making it a recursive array. 在Ruby中, 数组可以包含自身 ,使其成为递归数组。 Is it possible to put a JavaScript array inside itself as well?
是否有可能将JavaScript数组放入其中?
var arr = new Array();
arr[0] = "The next element of this array is the array itself."
Now how can I move arr
into arr[1]
so that the array contains itself recursively, (eg, so that arr[1]
is arr
, arr[1][1]
contains arr
, arr[1][1][1]
contains arr
, etc.)? 现在我如何将
arr
移动到arr[1]
以便数组递归地包含它自己(例如, arr[1]
为arr
, arr[1][1]
包含arr
, arr[1][1][1]
包含arr
等)?
Sure: 当然:
var a = [1];
a.push(a);
They're the same object: 他们是同一个对象:
a[1] === a[1][1] // true
And a convincing screenshot: 一个令人信服的截图:
Yes, sure: 是的,当然:
var x = [];
x.push(x);
console.log(x[0] === x); // true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.