简体   繁体   English

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

[英]What is the correct way to handle javascript objects referenced by one variable?

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.我有一个案例,我的 JavaScript 中有一个变量,该变量将用于存储对 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. class 创建了一个基于 div 的 window,有点像灯箱,然后当我调用类的 removeWindow 方法时,从 DOM 中删除了 div window 元素。 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.稍后在代码中,我再次创建了一个具有不同内容的 window,但我还希望能够检查 divWindow 变量是否引用了任何内容。

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?或者这是否会导致 JavaScript 垃圾收集出现 memory 问题? 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?如果我将 divWindow 设置为 null,这是否会向垃圾收集发出信号以收集我的 object 并摆脱它,或者我还需要做些什么来保持整洁?

As always, thanks in advance!一如既往,提前致谢!

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.例如,如果您在 divWindow 下方定义或表达了 function,这意味着垃圾收集器认为 divWindow 可以访问,至少只要 function 存在。 In that case, setting divWindow to null would actually cause the GC to clean it up on its next cycle.在这种情况下,将 divWindow 设置为 null 实际上会导致 GC 在下一个周期清理它。 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.在 javascript 中没有明确的方法来释放 memory。 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.您可以做的最好的事情是了解对 object 的所有引用,了解它的可访问性,并确保 object 在您不再需要时无法访问。 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.在你这样做之后,最终,你必须学会信任垃圾收集器,例如,当你编写想要在 IE 中运行的东西时,这并不容易。

(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.) (ps:我还想删除上面的评论。 delete运算符用于从 object 中删除属性。它不会“删除”变量,也不会释放 memory。事实上,ZDE9B9ED78D7E92E1DCEEFFEE78099你确实delete foo;在严格模式下。)

Doing the first solution is fine.做第一个解决方案很好。 This will signal the garbage collector, but it is infinitesimal to how much the garbage collector can take.这将向垃圾收集器发出信号,但对于垃圾收集器可以采取的量来说是无限小的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在单个JavaScript文件中处理多个页面上的事件的正确方法是什么? - What is the correct way to handle events on multiple pages in a single JavaScript file? 使用jQuery处理此数据的正确方法是什么? - What is the correct way to handle this data using jQuery? 仅处理承诺错误的正确方法是什么? - What is the correct way to ONLY handle a promise error? 在JSON中访问嵌套对象的正确方法是什么? - What is the correct way to access nested objects in JSON? 哪种方法正确? Javascript对象(nodejs) - which way is correct? Javascript Objects (nodejs) 发送对象数组的正确方法是什么,其中 object 的属性之一的值必须是文件? - What is the correct way to send an array of objects where the value of one of the properties of the object must be a file? 从对象的Javascript数组中为单个属性选择唯一值的正确方法是什么 - What is the correct way to select unique values for a single attribute out of a Javascript array of objects 如何检查是否引用了javascript变量,以及从何处? - How does one check if a javascript variable is referenced, and from where? 将变量附加到字符串的正确方法是什么? - What's the correct way to append a variable to a string? 检查全局变量是否存在的正确方法是什么? - What is the correct way to check if a global variable exists?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM