0

I have something like this

var myArray = [];
myArray.push(someObject);

but then if I delete or splice that array entry I just pushed it also deletes someObject(someObject was passed by reference by pushing, not a clone and I cannot have it be a clone). Is there some way I can:

  1. just remove the pointer to someObject from myArray without actually deleting someObject
  2. have it delete the actual key for the object in the array, but not shift all the other keys in the array?
4
  • What do you mean with "deletes someObject"? An object is only GC'd in JavaScript when it has no more references. So if something still points to it it's not lost. Commented Mar 8, 2012 at 23:49
  • What do you mean it "deletes someObject"? Any other variables that refer to the object should continue to do so. Commented Mar 8, 2012 at 23:49
  • Why do you think .splice deletes someObject? .splice() removes elements from an array but where those elements were objects it doesn't delete the actual objects - but you'd need to have some other reference to those objects to continue to use them. So if the someObject variable still refers to the object you can still use that after removing the reference from the array. .splice() actually returns an array of the deleted items, so... Commented Mar 8, 2012 at 23:50
  • instead of pushing the object to your array, you could push a clone $("#ele").clone() Commented Mar 8, 2012 at 23:50

1 Answer 1

1

someObject will not get deleted as long as some other variable or object in your javascript has a reference to someObject. If nobody else has a reference to it, then it will be garbage collected (cleaned up by the javascript interpreter) because when nobody has a reference to it, it cannot be used by your code anyway.

Here is a relevant example:

var x = {};
x.foo = 3;

var y = [];
y.push(x);
y.length = 0;   // removes all items from y

console.log(x); // x still exists because there's a reference to it in the x variable

x = 0;          // replace the one reference to x
                // the former object x will now be deleted because 
                // nobody has a reference to it any more

Or done a different way:

var x = {};
x.foo = 3;

var y = [];
y.push(x);      // store reference to x in the y array
x = 0;          // replaces the reference in x with a simple number

console.log(y[0]); // The object that was in x still exists because 
                   // there's a reference to it in the y array

y.length = 0;      // clear out the y array
                   // the former object x will now be deleted because 
                   // nobody has a reference to it any more
Sign up to request clarification or add additional context in comments.

Comments

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.