在JavaScript中,完成通过新ActiveXObject创建的对象后,是否需要将其设置为null? [英] in JavaScript, when finished with an object created via new ActiveXObject, do I need to set it to null?

查看:74
本文介绍了在JavaScript中,完成通过新ActiveXObject创建的对象后,是否需要将其设置为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WSH中运行并创建对象的Javascript程序中,比如说Scripting.FileSystemObject或任何任意COM对象,完成后是否需要将变量设置为null?例如,我是否建议这样做:

In a Javascript program that runs within WSH and creates objects, let's say Scripting.FileSystemObject or any arbitrary COM object, do I need to set the variable to null when I'm finished with it? Eg, am I recommended to do this:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var fileStream = fso.openTextFile(filename);
fso = null;  // recommended?  necessary? 
... use fileStream here ...
fileStream.Close();
fileStream = null;  // recommended? necessary?

效果与让var超出范围不一样吗?

Is the effect different than just letting the vars go out of scope?

推荐答案

为对象变量分配null将减少引用计数器,以便内存管理系统可以在感觉到它后立即丢弃该资源.当变量超出范围时,参考计数器将自动减小.因此,几乎在所有情况下,手动进行都是浪费时间.

Assigning null to an object variable will decrement the reference counter so that the memory management system can discard the resource - as soon as it feels like it. The reference counter will be decremented automagically when the variable goes out of scope. So doing it manually is a waste of time in almost all cases.

理论上,如果在中间将A设置为null,则在第一部分使用大对象A并在第二部分使用另一个大对象B的函数可能会提高内存效率.但是,由于这不会迫使彩信破坏A,因此该语句可能仍然是浪费.

In theory a function using a big object A in its first and another big object B in its second part could be more memory efficient if A is set to null in the middle. But as this does not force the mms to destroy A, the statement could still be a waste.

如果您进行一些精美的类设计,则可能会得到循环引用.然后可能需要手动打破圈子-但也许首先避免这样的循环会更好.

You may get circular references if you do some fancy class design. Then breaking the circle by hand may be necessary - but perhaps avoiding such loops in the first place would be better.

有传言称,古老的数据库访问对象带有错误,可以通过更改变量来避免这些错误.我不会基于这种伏都教来制定编程规则.

There are rumours about ancient database access objects with bugs that could be avoided by zapping variables. I wouldn't base my programming rules on such voodoo.

(Internet上有成千上万的VBscript代码,其中充斥着"Set X = Nothing";当被问到时,作者倾向于谈论习惯"和其他语言(C,C ++))

(There are tons of VBscript code on the internet that is full of "Set X = Nothing"; when asked, the authors tend to talk about 'habit' and other languages (C, C++))

这篇关于在JavaScript中,完成通过新ActiveXObject创建的对象后,是否需要将其设置为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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