处理一个变量引用的 javascript 对象的正确方法是什么?

发布于 2024-11-29 01:20:04 字数 700 浏览 0 评论 0原文

我的情况是,我的 JavaScript 中有一个变量,该变量将用于存储对某个类的多个实例的引用。该类创建一个基于 div 的窗口,有点像灯箱,然后当我调用该类的removeWindow 方法时,div 窗口元素将从 DOM 中删除。所以序列可能是这样的:

var divWindow = null;
divWindow = new DivWindowClass(...);
/* do some stuff with the window */
divWindow.removeWindow();

稍后在代码中我再次创建一个具有不同内容的窗口,但我也希望能够检查 divWindow 变量是否引用了任何内容。

这样做是否正确:

divWindow.removeWindow();
divWindow = null;

if (divWindow == null) { /* etc. */ }

/* later in the code */
divWindow = new DivWindowClass(...); /* with different content than first one */

还是会导致 JavaScript 垃圾收集出现内存问题?如果我将 divWindow 设置为 null,这是否会向垃圾收集发出信号来收集我的对象并删除它,或者我还需要做其他事情来保持整洁吗?

一如既往,提前致谢!

I have a case where I have a variable in my JavaScript that will be used to store the reference to several instances of a class. The class creates a div based window, kind of like a lightbox, and then when I call the class' removeWindow method, the div window elements are removed from the DOM. So a sequence might be like this:

var divWindow = null;
divWindow = new DivWindowClass(...);
/* do some stuff with the window */
divWindow.removeWindow();

Later on in the code I create a window again with different content, but I also want to be able to check if the divWindow variable is referencing anything.

Is it correct to just do:

divWindow.removeWindow();
divWindow = null;

if (divWindow == null) { /* etc. */ }

/* later in the code */
divWindow = new DivWindowClass(...); /* with different content than first one */

or does that cause memory issues with the JavaScript garbage collection? If I set divWindow to null, will that signal to the garbage collection to gather up my object and get rid of it, or is there something else I need to do to be tidy?

As always, thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给不了的爱 2024-12-06 01:20:04

好吧,这取决于。例如,如果您在 divWindow 下定义或表达了一个函数,则意味着垃圾收集器认为 divWindow 是可访问的,至少只要该函数存在即可。在这种情况下,将 divWindow 设置为 null 实际上会导致 GC 在下一个周期清理它。但是,如果情况并非如此,“清空”变量将不会比正常情况更有帮助。如果您发布了完整的代码,我可能可以告诉您更多信息。

JavaScript 中没有明确的方法来释放内存。您能做的最好的事情就是了解对对象的所有引用,了解它的可访问性,并确保当您不再需要该对象时无法访问该对象。完成此操作后,最终您必须学会信任垃圾收集器,例如,当您编写想要在 IE 中运行的代码时,这并不那么容易。

(ps:我还想删除上面的注释。delete运算符用于从对象中删除属性。它不会“删除”变量,也不会释放内存。事实上,如果您在严格模式下执行delete foo;,JavaScript 将会抛出异常。)

Well it depends. If you have a function defined or expressed below the divWindow for example, that would mean divWindow is considered reachable by the garbage collector, at least for as long as that function exists. In that case, setting divWindow to null would actually cause the GC to clean it up on its next cycle. However, if that's not the case, "nulling" a variable won't help it anymore than normal. If you posted the entirety of your code, I could probably tell you more.

There is no explicit way to free up memory in javascript. The best you can do is to be aware of all references to your object, be aware of how reachable it is, and make sure the object is unreachable when you don't need it anymore. After you do that, ultimately, you have to learn to trust the garbage collector, which isn't so easy when you're coding something that you want to run in IE for example.

(ps: I also want to knock out a comment made above. The delete operator is for removing properties from an object. It doesn't "delete" variables, and it doesn't free up memory. In fact, javascript will throw if you do delete foo; in strict mode.)

我家小可爱 2024-12-06 01:20:04

执行第一个解决方案就可以了。这将向垃圾收集器发出信号,但对于垃圾收集器可以采取的数量而言,它是无穷小的。

Doing the first solution is fine. This will signal the garbage collector, but it is infinitesimal to how much the garbage collector can take.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文