admin管理员组文章数量:1289515
var domElementReference = $(document.createElement('div'));
Will the DOM element get destroyed if I don't actually insert it on the page (once domElementReference
gets out of scope)?
If not: If I have a constructor-function that creates DOM elements, is there an automatic way to clear them in javascript?
What I tought was to append them on an element, and then use
myChildNode.parentNode.removeChild(myChildNode);
But than again I have to manually call the function when the object is getting out of scope, and it kind of messes up the whole 'garbage-collection' idea. Any patterns to automatically destroy the object?
var domElementReference = $(document.createElement('div'));
Will the DOM element get destroyed if I don't actually insert it on the page (once domElementReference
gets out of scope)?
If not: If I have a constructor-function that creates DOM elements, is there an automatic way to clear them in javascript?
What I tought was to append them on an element, and then use
myChildNode.parentNode.removeChild(myChildNode);
But than again I have to manually call the function when the object is getting out of scope, and it kind of messes up the whole 'garbage-collection' idea. Any patterns to automatically destroy the object?
Share Improve this question edited Apr 14, 2014 at 10:17 asked Apr 14, 2014 at 10:04 user1546328user15463281 Answer
Reset to default 12If the elements haven't been inserted into the DOM and no other references exist, then yes they will be garbage collected, just like any other variables.
Modern browsers use a Mark-and-sweep algorithm for garbage collection, this means the garbage collector will look for and garbage collect objects that are unreachable. If you create elements in your function, but don't assign a reference elsewhere or don't insert them into the DOM, then they will be eligible for garbage collection after the function pletes.
There is no need to manually try to free memory in JavaScript, it is all handled implicitly.
- MDN documentation for Memory Management in JavaScript.
本文标签: Do DOM objects get garbage collected in javascriptStack Overflow
版权声明:本文标题:Do DOM objects get garbage collected in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741474324a2380790.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论