object1 as a property of object2 which is in turn a property of object1 - will result in memory leak?

丶灬走出姿态 提交于 2019-12-19 10:52:53

问题


Will "form" and "form.errorProcessor" just hold single references to each other and have only 2 objects in memory, or is this a leak / problem situation?

var ErrorProcessor = function(form){
    this.form = form; // Problem Line
}
var form = $("form");
form.errorProcessor = new ErrorProcessor(form); // Some element assignment

Post the problem line, I have an object hierarchy as below [checked by console.log(form)]

form: {errorProcessor: {form: {errorProcessor: {form: {errorProcessor: {...}}}}}}


回答1:


No, there is no memory leak caused by circular references, and you are creating only two objects in your code. Any decent garbage collector can handle them.

The only problem is when you recursively inspect your object (like you do with expanding its properties in the console), you go down a bottomless pit - if done programmatically, you'd get an infinite loop or stack overflow from recursion. All these structures you see in your console are representing the same object, though.



来源:https://stackoverflow.com/questions/26161240/object1-as-a-property-of-object2-which-is-in-turn-a-property-of-object1-will-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!