清除数组时,数组中的JavaScript对象是否会从内存中擦除? [英] Do javascript objects inside of an array erase from the memory when I clear the array?

查看:97
本文介绍了清除数组时,数组中的JavaScript对象是否会从内存中擦除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有真正想过垃圾收集,我不知道在制作小型的JavaScript游戏/应用程序时是否需要考虑垃圾收集。任何建议都会受到赞赏,但我会在最后提出具体问题。



很多时候我编写这种形式的代码:

  var foos = new Array(); 
generateFoos();
function generateFoos()
{
foos = []; (fooIndex = 0; fooIndex< numberOfFoos; fooIndex ++)
{
foos [fooIndex] = new foo(Math.random(),Math.random());


函数foo(bar,bas)
{
this.bar = bar;
this.bas = bas;





$ b

所以我的问题是,当我说 foos = [] (第5行),这是否会从内存中删除该数组中的对象,或者它们是否在某处浮动,从而使得程序变得越来越慢?我应该怎么做,如果我想调用 generateFoos()一次循环次数,就像每次用户按下一个键一样。



谢谢!

解决方案

对于特定的答案,由于接受的答案实际上并不回答问题直接的,是的,foo = []不会去引用数组中的任何以前的值。

正如Ales所说:一个对象变成符合条件垃圾收集时,它变得无法访问。事实上,浏览器会从内存中清除这些内容。



重要的一点是,删除不会垃圾收集



你一遍又一遍地看到这个,甚至在这个问题的评论中。 删除关键字从对象中删除属性,与垃圾收集无关。



我也想为您的代码本身提供一些建议。

1 )对基本数据类型使用文字而不是新的。

2)在声明它们之前不要调用函数。是的,它可行,但稍后会变得杂乱难读。请记住,你花更多的时间来阅读你的代码,而不是写它。稍后再轻松一点。

3)记住你的函数范围。任何没有var的变量都是全局变量。使用var时,它仍然在包含它的函数的范围内。另一种方法是在函数中作为变量的范围是当它们作为命名参数传递时。



4)创建函数时使用var函数。在你的代码中,你的函数是全局变量。



5)使用间距。文字的密度并不靠近敬虔。你可能已经有了20件东西,眼睛很好,但是在短短的几年里你会欣赏白色空间。

6)用var声明计数器for循环。除非你希望他们成为全球。而且你几乎没有 会。



让我们现在重新编写代码:

  var numberOfFoos = 10,
foos = [];

var generateFoos = function(){

foos = []; (var fooIndex = 0; fooIndex< numberOfFoos; fooIndex ++){

foos [fooIndex] = new foo(Math.random(),Math.random())为

。 );


$ b},

foo =函数(bar,bas){

this.bar = bar;
this.bas = bas;

}

generateFoos();
console.log(foos);


I never really gave much thought to garbage collection and I don't know whether or not it is necessary to take into account when making small javascript games/applications. Any advice is appreciated, but I will ask my specific question at the end.

A lot of the time I write code of this form:

var foos=new Array();
generateFoos();
function generateFoos()
{
    foos=[];
    for (fooIndex=0;fooIndex<numberOfFoos;fooIndex++)
    {
        foos[fooIndex]=new foo(Math.random(),Math.random());
    }
} 
function foo(bar,bas)
{
   this.bar=bar;
   this.bas=bas;
}

So my question is, when I say foos=[] (line 5), does this delete the objects in that array from the memory or do they float around somewhere, making the program larger and slower? What should I do if I want to call generateFoos() a loooot of times, like every time the user presses a key.

Thanks!

解决方案

For a specific answer, since the accepted one doesn't actually answer the question directly, is that yes, foo = [] does de-reference any previous values in the array.

As Ales says, "An object becomes eligible for garbage collection when it becomes unreachable." Indeed, this is when the browser will clear such things from memory.

An important point, delete DOES NOT GARBAGE COLLECT.

You see this over and over, and even in the comments on this question. The delete keyword removes a property from an object and has nothing to do with garbage collection.

I also wanted to offer some advice on your code itself.

1) Use literals, not new, for basic data types

2) Don't call functions before you declare them. Yes, it works but its messy and harder to read later. Remember, you spend much more time reading your code than writing it. Make it easy to follow later.

3) Remember your function scope. Any variable declared without var goes global. With var, it remains within the scope of the function that contains it. Another way variables are scoped within a function is when they are passed in as named parameters.

4) Use var on your functions when creating them. In your code, your functions are globals.

5) Use spacing. Density of text is not next to godliness. You might be 20-something now with great eyesight, but you'll appreciate white space in just a very few short years.

6) Declare counters in for loops with var. Unless you want them to be global. And you almost never will.

Lets re-work your code now:

var numberOfFoos = 10,
    foos = [];

var generateFoos = function(){

    foos = [];

    for( var fooIndex = 0; fooIndex < numberOfFoos; fooIndex++ ){

        foos[ fooIndex ] = new foo( Math.random(), Math.random() );

    }

},

foo = function( bar, bas ){

   this.bar = bar;
   this.bas = bas;

}

generateFoos();
console.log( foos );

这篇关于清除数组时,数组中的JavaScript对象是否会从内存中擦除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆