correctly resetting a global variable in javascript?

12,159

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

var A=[1,2,3];
A=undefined;
console.log(A); //undefined

Solution 5

var handle={};
handle.A=[1,2,3,4,5,{x:2,y:3}];
console.log(handle);

what is given now

delete handle.A;
console.log(handle); //A is gone!

after deleting A from handle

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..

so be carefull

Share:
12,159
tim
Author by

tim

Updated on June 04, 2022

Comments

  • tim
    tim almost 2 years

    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?