I am looking at some JS ViewModel code that uses this pattern to be able to reference the outer closure in case whatever library changes the “this” pointer when invoking a function as a callback (gotta love 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 implementation specific? (e.g. if garbage is collected inside out versus outside in)
Edit: Please assume that at the point when the GC considers this object for collection nothing holds a reference to someCallBack.
The garbage collector is clever enough to detect such cycle references. So it won’t leak.