JavaScript: document.createElement('') &删除 DOM 元素

发布于 2024-08-13 06:51:40 字数 372 浏览 4 评论 0原文

如果您在函数中创建一个元素,例如:

function makeDomElement()
{
   var createdElement = document.createElement('textarea');
}

并且您没有将其附加到 DOM 中的任何位置(即通过 .appendChild 函数),它是否仍然保留在内存中?那么你必须这样做吗?

function makeDomElement()
{
   var createdElement = document.createElement('textarea');
   delete createdElement;
}

我只是好奇:)

If you create a element within a function like:

function makeDomElement()
{
   var createdElement = document.createElement('textarea');
}

And you do not append it anywhere in the DOM i.e. via .appendChild functions, does it still remain in memory? So would you have to do

function makeDomElement()
{
   var createdElement = document.createElement('textarea');
   delete createdElement;
}

I'm just curious :)

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

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

发布评论

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

评论(2

半衬遮猫 2024-08-20 06:51:40

它会因浏览器的不同而有所不同,但是 javascript delete 关键字与 DOM 的 createElement 方法无关。无需使用删除

将会发生的情况是,对 createdElement 当前保存的元素的引用将被垃圾收集。现在,对于 IE,这意味着该元素的引用计数将降至 0,因此它将销毁自身并释放其内存。其他浏览器的处理方式有所不同,通常 DOM 中的元素本身就是垃圾收集对象,并且将在同一(或者可能是特定于 DOM 的)GC 周期中被删除。

如果该元素已添加到文档中,那么在 IE 中,将会向该元素添加另一个引用,因此当删除 createdElement 中的引用时,元素对象仍将具有非零引用计数并继续存在。

对于元素本身被垃圾收集的其他浏览器,该元素不会被收集,因为收集器会在连接到文档的对象图中看到它。

It will vary from browser to browser however the javascript delete keyword has nothing to do with the DOM's createElement method. There is no need to use delete.

What will happen is that the reference to the element currently held in the createdElement will get garbage collected. Now in the case of IE that will mean that the element will have its reference count dropped to 0 so it will destroy itself and release its memory. Other browsers do things differently typically the elements in the DOM are themselves garbage collected objects and will be removed during the same (or perhaps a DOM specific) GC cycle.

Had the element been added to the document then in the case of IE there would be another reference added to the element so when the reference in createdElement is removed the element object would still have a non-zero reference count and continue to exist.

In the case of other browsers where the elements themselves are garbage collected the element wouldn't be collected since the collector would see it in the graph of objects connected to the document.

一梦浮鱼 2024-08-20 06:51:40

函数终止后,不再有任何对该对象的引用,即,如果垃圾收集器正常工作,则应该收集该对象(如果涉及 DOM 节点,则存在一个 IE 错误,该错误会阻止收集具有循环引用的对象)。

另外,你的代码也被破坏了,因为局部变量无法被删除:尝试这样做甚至会在严格模式 ES5 中抛出语法错误。

After the function terminates, there's no longer any reference to the object, ie if the garbage collector works properly, it should be collected (there's an IE bug that prevents objects with circular references to be collected if DOM nodes are involved).

Also, your code is broken as local variables can't be deleted: trying to do so will even throw a syntax error in strict mode ES5.

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