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?
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?
-
1
an object could still have its prototype, and it must! thats what javascript is. objects of prototypes, empty or not! because even
null
is something, it is nothing, so also[]
is something, butvar A=undefined
is undefined, but A as object of some proto/type exist! – Ol Sen Commented Apr 24, 2013 at 23:33 -
@alex23, thanks for referring to that other post. It seems that
myglobals.A.splice(0)
is actually not really the best way. From the accepted answer there, I glean that it should be set tonull
in order to allow the browser to collect the memory, right? – tim Commented Apr 25, 2013 at 0:53 -
@codelio, perhaps I am not asking my question well, sorry about that if it's the case. Your answer below doesn't sound right: if I set
A=undefined
, thentypeof(A)
returnsundefined
but I expect to getobject
– tim Commented Apr 25, 2013 at 1:03 -
null
is not preparingA
to free its taken memory, because its null. have look atvar A=1; A=void(A)
, thats what you mean perhaps, but it's still valid A, the same asvar A
is in this moment. javascript frees memory if the objects/value 's parent is set so that it does not have anymore that child. – Ol Sen Commented Apr 25, 2013 at 2:19 -
console.log(a.constructor.name, typeof a);
//"Array", "object" – Ol Sen Commented Apr 25, 2013 at 3:05
5 Answers
Reset to default 6You 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 bees a new reference, to a different array.
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
.
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.
var A=[1,2,3];
A=undefined;
console.log(A); //undefined
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..