I’ve been using javascript module pattern for while.
I showed an example of a module pattern to one of my coworkers. He said that the following code can make a memory leak.
var test = (function(){
var events = {
// my functions go here
}
return {
// return something
}
}());
he said that since events variable is an object and i’m not setting null for it, it can cause a memory leak even if I set null for test later.
As far as I know, the above code snippet is okay because I’m not passing events around.
I need advices!
I think your code is fine. Memory leaks in closures happen when you have a reference to a DOM element, because of the circular reference (
fooholds on toelement, andelementholds on tofoo, so they can never be garbage collected):My source for this belief is here
But others might know of another problem …