Assign a closure reference to a variable inside the closure - leak?

I am looking at some JS ViewModel code that uses this template to be able to refer to an external closure if any library changes the "this" pointer when calling the function as a callback (must like JavaScript):

function FruitViewModel { var that = this; // <-- HERE this.someCallBack = function () { that.utilityFunction(); }; this.utilityFunction = function () { } } 

My question is: will this cause a memory leak or will it be specific? (e.g. if garbage collects inside out and out)

Edit: suppose that the moment the GC considers this object for a collection, nothing contains a reference to someCallBack .

+4
source share
2 answers

The garbage collector is smart enough to detect such loop references. Therefore, it will not flow.

+1
source

In all JS systems, the object will not be assembled until all functions declared in the parent function are no longer referenced.

I do not know about any GC errors that matter here.

+1
source

All Articles