views:

95

answers:

4

As far as i can see in a situation like this:

var x = [];
var y = {};

y.someProp='asd';

This doesnt work:

x.push(y);

What I want to do is add a reference of y to x so that later if I will "delete y;" I want it also to be removed from the array x.

A: 

That's not how delete works. It will delete the object reference you pass to it - not any other references to the same object. You're looking for something like a "weak reference" here and I'm not aware of any ways of implementing those in JavaScript.

Evgeny
A: 

Would this help?

Var x=[{}]; var y=x[0];

Edit: Sorry, I was a bit brief in my first attempt at answering. What I'd start asking is: why does y need to "exist" outside of the array in the first place? Can't you just create an array of objects and use delete or array.splice() to remove the object when you need to?

Second, if y needs to exist outside of the array, what you need to do is either to create a property that refers to the array index, or a utility function that can scan through the array to find the correct object reference to remove. In other words, something like

var x=[]; var y={}; y.arrayIndex=(x.push(y)-1);

// later on, you want to get rid of y

delete x[y.arrayIndex]; delete y;
hallvors
y is an object passed by reference to a function so I cant use something like this.
intacto
You were quit quick at responding :), meanwhile I tried to improve my suggestion.
hallvors
(and even if you pass the object to the function it would work fine to do func(x[0]); - no?)
hallvors
it wont work because if you delete some elements from x, arrayIndex will become just (arraylength-1) not the index of the new added(pushed) element
intacto
+1  A: 

You are mixing up variables, references and objects.

Doing delete y; removes the variable y. As the variable no longer exists, it will naturally no longer have a value. Thus the reference that the variable contained is gone.

Removing the variable will however not in itself remove the object that it was referencing. The array still contains a reference to the object, and neither of those depend on the existance of the variable.

There is actually no way of removing the object directly. You remove objects by destroying all references to it, not the other way around. You want the object to remove it's references, which is simply not how it works.

Guffa
It will be enough for me if I can just remove the reference in the array to the object from an event of the object itself.Can I somehow pass a reference of reference to the object?
intacto
@Guffa, `delete y` only works in the global context, when `y` is `this.y`. Read this: http://perfectionkills.com/understanding-delete/
J-P
@intacto: To even make it possible for the object to remove the reference from the array, the object would have to contain references to all arrays that contains references to it. So, when adding the object to an array, you also have to add the array to the object...
Guffa
@J-P: Yes, I know, but that's not really relevant to the question.
Guffa
A: 

This doesnt work:

x.push(y);

Of course it does, it will work exactly as intended - pushing the value of y to the array you created.

The delete operator is specifically for deleting properties on objects, if the expression following the operator does not resolve to a property then nothing happens at all.

var y = {"someprop":true};

delete y;
alert(y.someprop); // alerts "true"

Try it in your browser window, paste the following into your address bar and hit enter:

javascript:var y = {"someprop":true}; delete y; alert(y.someprop);
Andy E
I got it thank you. is there a way that I can get the index of the newly pushed element?
intacto
@intacto: `push()` appends elements to the end of the array and the return value is the new length, so if only one element is added the index would always be `array.push(item) - 1`, e.g. `var index = x.push(y) - 1;`
Andy E