javascript - Remove item in array without deleting the object it points to -
i have this
var myarray = []; myarray.push(someobject);
but if delete or splice array entry pushed deletes someobject(someobject passed reference pushing, not clone , cannot have clone). there way can:
- just remove pointer someobject myarray without deleting someobject
- have delete actual key object in array, not shift other keys in array?
someobject not deleted long other variable or object in javascript has reference someobject. if nobody else has reference it, garbage collected (cleaned javascript interpreter) because when nobody has reference it, cannot used code anyway.
here relevant example:
var x = {}; x.foo = 3; var y = []; y.push(x); y.length = 0; // removes items y console.log(x); // x still exists because there's reference in x variable x = 0; // replace 1 reference x // former object x deleted because // nobody has reference more
or done different way:
var x = {}; x.foo = 3; var y = []; y.push(x); // store reference x in y array x = 0; // replaces reference in x simple number console.log(y[0]); // object in x still exists because // there's reference in y array y.length = 0; // clear out y array // former object x deleted because // nobody has reference more
Comments
Post a Comment