Reputation: 3913
so, in this post here people are discussing the fact that
A = [1,2,3];
and then doing
A = [];
will not reset the array but create a new one.
My question is, if I use a global object variable
myglobals = { A : [] }
Can I safely reset the array with
myglobals.A = [];
Right? Since that's referencing the same object property and thus I'm not actually creating a new array, am I?
Update to question due to remarks below
Since there is a general consensus that splice(0)
is the way to go, and since a very similar question has an answer that explains the impact to browser freeing up memory, I'm wondering if it's generally safe and proper to set any defined object (whether array or function or string, etc...) to null
in order to reset it's value while retaining it's reference?
Upvotes: 2
Views: 14548
Reputation: 3368
var handle={};
handle.A=[1,2,3,4,5,{x:2,y:3}];
console.log(handle);
delete handle.A;
console.log(handle); //A is gone!
why is delete sometime better? it really kills A
from handle
, following could confuse you in working with larger objects, if you hope .length
is telling you the truth.
handle.A=[null];
handle.B=undefined;
handle.C=null;
handle.A.length=10;
console.log(handle, handle.length, handle.A.length);
not very save in coding, because i set it =10
and
your script could assume wrong there is something to loop thru 10 elements.
so handle.A=null
will help, but not really changing the structure of your object.
but it can also not break your script because you have something like var A
, and you could prevent loops thru not existing elements with it. same works with set to undefined
, se below..
Upvotes: 1
Reputation: 187034
Not really...
// make a global variable
var a = [1,2,3];
// Assign it to something
var someObj = { value: a };
someObj.value; // [1,2,3];
// set a new value for the global
a = [];
a; // []
someObj.value; // [1,2,3];
This is the initial code you mention. You can change the object the global variable points to, but you can't change other reference to the object you are replacing.
And the same problem exists with your second example:
// make a global variable
var globals = { a: [1,2,3] };
// Assign it to something
var someObj = { value: globals.a };
someObj.value; // [1,2,3];
// set a new value for the global
globals.a = [];
globals.a; // []
someObj.value; // [1,2,3];
You would have to reference the globals
container object if you want references to be updated. That is other object hold a reference to the container, and then you can change the contents of that container.
// make a global variable
var globals = { a: [1,2,3] };
// assign a reference to the container in another object.
var someObj = { globals: globals };
someObj.globals.a; // [1,2,3];
// set a new value for the global
globals.a = [];
globals.a; // []
someObj.globals.a; // [];
Thought that can get a bit unwieldy.
You could also alter the object reference by the global, rather than replacing it.
var a = [1,2,3];
var b = a; // a and b now reference the same object.
a.splice(0); // remove all items from this array, without replace the array object.
a; // [];
b; // [];
// a and b now still point to the same array, which is now empty.
Upvotes: 2
Reputation: 71918
You are creating a new array. If you want to truncate the array, just set its length to zero:
var a = [1,2,3];
a.length = 0;
// a is now []
In your example with object properties, it's just the same. There are no pointers in JavaScript, just reference values. So the value of myglobals.A
is a reference to an array. When you assign a new array to it, that value becomes a new reference, to a different array.
Upvotes: 6
Reputation: 3530
Nope, this still creates a new array. The important factor is the assignment, not the scope to which the "A" variable is attached. As long as you do something that looks like something = []
, the JavaScript engine is going to manufacture a new Array object (the []
part) then assign a reference to it to something
.
Upvotes: 2