6
var a = {
    "example" : true
};

var x = [a], y = [a];

delete x[0];

console.log(y);

In the above code, would it be possible to have a deleted, not just the reference in x ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
glider
  • 63
  • 1
  • 3

2 Answers2

10

That's up to the garbage collector. As long as there's some reference to the object, it will not be garbage collected.

If you want it to be cleaned up, make sure there are no more references.

So to answer your question, no, there's no way to explicitly destroy an object. If a and y[0] are still referencing it, you can't do it from your x variable.

To be clear, x[0] is not referencing a. It is pointing to the same object in memory that a is referencing.

user113716
  • 318,772
  • 63
  • 451
  • 440
  • So, there is no way of removing a from y, without knowing the index ? – glider Feb 23 '11 at 21:43
  • 1
    @glider: Aside from dereferencing all the items in the `y` Array, or iterating over the Array to look for it (or using `.indexOf()` if supported), then not really. – user113716 Feb 23 '11 at 21:45
1

i think you should refer this question.

Deleting Objects in JavaScript

Community
  • 1
  • 1
Praveen Prasad
  • 31,561
  • 18
  • 73
  • 106