Javascript:document.createElement('')&删除DOMElement [英] Javascript: document.createElement('') & delete DOMElement

查看:590
本文介绍了Javascript:document.createElement('')&删除DOMElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

您不会将它附加到DOM中的任何位置,即通过.appendChild函数,它仍然留在内存中吗?所以你必须这样做:

$ p $ 函数makeDomElement()
{
var createdElement = document.createElement( 'textarea的');
delete createdElement;
}

我只是好奇:)


<它不同于浏览器,但是javascript delete 关键字与DOM的 createElement 方法。没有必要使用 delete



会发生什么情况是对当前保存在 createdElement 将被垃圾收集。现在在IE的情况下,这将意味着该元素将其引用计数降到0,因此它会破坏自身并释放其内存。其他浏览器以不同的方式做事,通常DOM中的元素本身就是垃圾收集的对象,并且会在相同的(或者是特定于DOM的)GC循环中被删除。

元素被添加到文档中,那么在IE的情况下将会有另一个引用被添加到元素,所以当 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 :)

解决方案

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.

这篇关于Javascript:document.createElement('')&amp;删除DOMElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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