最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - correctly resetting a global variable in javascript? - Stack Overflow

programmeradmin3浏览0评论

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?

Share Improve this question edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked Apr 24, 2013 at 22:45 timtim 3,9135 gold badges36 silver badges39 bronze badges 5
  • 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, but var 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 to null 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, then typeof(A) returns undefined but I expect to get object – tim Commented Apr 25, 2013 at 1:03
  • null is not preparing A to free its taken memory, because its null. have look at var A=1; A=void(A), thats what you mean perhaps, but it's still valid A, the same as var 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
Add a ment  | 

5 Answers 5

Reset to default 6

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

发布评论

评论列表(0)

  1. 暂无评论