javascript 在javascript中正确重置全局变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16203494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:41:35  来源:igfitidea点击:

correctly resetting a global variable in javascript?

javascriptarraysvariablesscope

提问by tim

so, in this post herepeople 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 nullin order to reset it's valuewhile retaining it's reference?

由于有一个普遍的共识splice(0)是要走的路,并且由于一个非常相似的问题有一个解释对浏览器释放内存的影响的答案,我想知道设置任何定义的对象(无论是数组或函数或字符串等...)以null在保留其引用的同时重置其

回答by bfavaretto

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.Ais a reference to an array. When you assign a new array to it, that value becomes a new reference, to a different array.

在您的对象属性示例中,它是一样的。JavaScript 中没有指针,只有引用值。所以 的值myglobals.A是对数组的引用。当您为其分配一个新数组时,该值将成为对不同数组的新引用。

回答by Alex Wayne

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

globals如果要更新引用,则必须引用容器对象。即其他对象持有对容器的引用,然后您可以更改该容器的内容。

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

回答by Jollymorphic

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.

不,这仍然会创建一个新数组。重要的因素是赋值,而不是“A”变量所附加的范围。只要您执行类似于 的操作something = [],JavaScript 引擎就会生成一个新的 Array 对象([]部件),然后将对其的引用分配给something

回答by Ol Sen

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

回答by Ol Sen

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

从句柄中删除 A 后

why is delete sometime better? it really kills Afrom handle, following could confuse you in working with larger objects, if you hope .lengthis telling you the truth.

为什么删除有时更好?它真的杀死Ahandle,下面可能混淆你与较大的物体的工作,如果你希望.length告诉你真相。

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 =10and your script could assume wrong there is something to loop thru 10 elements. so handle.A=nullwill 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..

在编码方面不是很节省,因为我设置了它=10并且您的脚本可能会假设错误,有一些东西可以通过 10 个元素循环。所以handle.A=null会有所帮助,但不会真正改变对象的结构。但它也不会破坏你的脚本,因为你有类似的东西var A,你可以防止循环通过不存在的元素。同样适用于 set to undefined, se 下面..

so be carefull

所以要小心