更好地接近零化变量,javascript中的对象 [英] Better approach nulling variables, objects in javascript

查看:120
本文介绍了更好地接近零化变量,javascript中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一些移动设备,并希望以某种方式清除,空对象,变量以释放一些内存。这里我有两个简单的例子,我认为这两个都是匿名函数但是哪种方式更好或更有效?对不起,如果我弄错了。对我来说,两者似乎做同样的事情,虽然我更喜欢第一个,因为在我需要它之前不会创建对象。第二个版本将立即执行用于创建变量,对象等的代码,但在我需要它之前不会执行主构建函数。

I am building something for mobile and would like somehow to clear, null objects, variables to release a bit of memory. Here I have two quick examples, both are anonymous functions as I believe but what way is better or more valid approach? Sorry if I get it all wrong. To me both seem to do the same thing although I like more the first one as objects wont be created until I need it. The second version would immediately execute the code for creating variables, objects etc. but not doing the main build function until I need it.

我只想弄清楚是什么方式更常见。我知道像我这样的初学者大多误解了匿名函数的使用。感谢您的贡献。

I am just trying to figure out what way is more common. I know that beginners like me mostly misunderstand the use of anonymous functions. Thank you for your contribution.

V1

var app = function() {

        //create variables, objects
        var a = 'Data 1';
        var b = 'Data 2';

        //do some things    
        console.log(a + ', ' + b);

        //do a cleanup
        app.cleanup = function() {

            a = null;
            b = null;
            console.log(a, b);

        }
    }
    setTimeout(app, 200);

V2

var app = {};

    (function(){

        //create variables, objects
        var a = 'Data 1';
        var b = 'Data 2';

        app.build = function(){                 

            //do some things
            console.log(a + ', ' + b);          

        }

        //do a cleanup
        app.cleanup = function(){

            a = null;
            b = null;
            console.log(a, b);

        }   

        setTimeout(app.build,200);

    })();

以后的html或事件

<input type="button" onclick="app.cleanup()" value="clean" />


推荐答案

你不应该担心释放资源。 JavaScript有一个垃圾收集器,它将获取超出范围的变量并销毁它们。使用删除obj.yourReference reference = null 之类的东西,在不需要时删除对象的引用类似的,让垃圾收集器做其余的事情。

You shouldn't be worrying about freeing up resources. JavaScript has a garbage collector which will pick up variables which fall out of scope and destroy them. Delete a reference to an object when you don't need it using delete obj.yourReference, reference = null or something similar, and let the garbage collector do the rest.

你会发现#1会自动收回 a b app.cleanup()定义,code>变量本身会自动生成。除非你这样做, a b 都包含在由清理创建的闭包中你要留下的功能,所以你要阻止垃圾收集器做这件事。

You'll find that #1 will automatically reclaim the a and b variables itself automatically, if you remove your app.cleanup() definition. Unless you do that, a and b are both enclosed in the closure created by the cleanup function you're leaving behind, so you're stopping the garbage collector from doing it's thing.

摆脱整个<$在$ 1中c $ c> app ,您必须执行删除window.app app = null as window保存对它的引用。

To get rid of the whole app in #1, you'll have to do delete window.app or app = null as window holds a reference to it.

这篇关于更好地接近零化变量,javascript中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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