0

My question is related to circular references in JavaScript. Basically I am trying to create a js array and put the same array as a property inside itself. While I tried the below code on my firebug console, i could not reason why the below outputs happened.

var a = [];
a[a]=a;

//delete(a[a[a[a]]]); edited
delete(a[a][a][a][a]);

console.log(a[a]);
//says undefined. but i deleted the 4th nested level ??

console.log(a);
//still gives []. so a exists.

My question is, I deleted only a[a][a][a][a], so but the outer properties should still stay right? like a[a] & a[a][a] should not be undefined right?.

edit

but when i assign the array as a property with key as itself and inspected the same on firebug, I am able to expand both the key and value of the object in nested fashion, If it gets as null as key it should be visible in the inspector right?

1
  • 2
    Javascript assigns arrays by reference, not by value. Commented Apr 26, 2014 at 15:52

2 Answers 2

3
  • As you say it's a circular reference - so there is no "4th nested level" but only one which goes back to itself
  • You shouldn't use a as a property here. Property names must be strings in JS, and it will just stringify [] to "" automatically when using the empty array as a property name. You're lucky the stringification did not take the non-numeric property into account.

So what happens:

var a = [];
// OK an empty array.

a[a] = a;
a[""] = a;
// set the "" property of it to itself

delete(a[a[a[a ]]]);
delete a[a[a[a ]]]
delete a[a[a[""]]]
delete a[a[ a   ]]
delete a[a[ ""  ]]
delete a[ a      ]
delete a[ ""     ]
// remove the "" property from a

console.log(a[a]);
// is undefined now again

console.log(a);
// still gives []. Is still an array without any items.

Notice that a[a][a][a] === a so when you do delete a[a][a][a][a] it's just equivalent to delete a[a]. You can follow a circular reference infinitely often, but it is only one reference that exists and then gets deleted:

a[""] = a;
// set the "" property of it to itself again

delete(a[""][""][""][""]);
delete    a[""] [""] [""] [""]
delete (((a[""])[""])[""])[""]
delete ((  a    [""])[""])[""]
delete (        a    [""])[""]
delete               a    [""]
// remove the "" property from a again

console.log(a[""]);
// is undefined now again
Sign up to request clarification or add additional context in comments.

3 Comments

i edited the question. what i meant was a[a][a][a][a]. u are right regarding the other one.
when i did the step a[a] = a; and inspected it in firebug. I can expand both key and its value in the same nested fashion. So the key getting converted to null is not visible there. Or am i wrong?
no idea what you are stepping in at, but when you evaluate the property reference then the a in [a] is getting stringified to the empty string. Maybe firebug shows the value of the a variable to be the nested object, but that does make absolutely no difference. You can replace every a in a property access with "" and your script will work in the same way.
1

What happens is like this:

  • a has a reference to itself.
  • a[a] gets the value that is located at the string value of a which is a reference to a.
  • a[a[a]] get the value that is located at the string value of the result of a[a] which is a reference to a.

So you can tell that there is no true "4th level".

The delete reduces to delete(a[a]).

Update:

Since

a[a][a][a][a] = a[a][a][a] = a[a][a] = a[a] = a;

because a[a] returns a and you just apply the [] operator to a again.

So

delete(a[a][a][a][a]); becomes delete(a[a]); which becomes delete(a[""]);

Update 2:

Have a look at this fiddle.

var a = [];
a[a] = a;
console.log(a); // has 1 element
console.log(a[a] == a); //true
console.log(a[a] === a); // true
console.log(a == ""); // true

a[a][a][a].push("1");
console.log(a); // has 2 elements
console.log(a[a]); // this becomes undefined because a is no more ""

var b = [];
b[b] = b;
b.push("1");
console.log(b);
console.log(b[b]);
console.log(b[b] == b);

2 Comments

i edited the question. what i meant was a[a][a][a][a].
+1: "i have bit more doubt in understanding the same. Pls see my edit in qn."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.