CoderBear's Blog
Memory leaks in JavaScript III
July 6, 2015

Breaking circular references
Once these patterns discussed in the previous post are identified, circular references can be worked around (pun intended).

JS Bin on jsbin.com

Assigning null to elem at the end breaks the circular reference.


Use another closure

JS Bin on jsbin.com

Enclosing anotherInnerFunction in brackets and adding another pair of brackets at the end means that the function will get executed as soon as the code loads. It is known as an IFFE or an Immediately Invoked Function Expression. This IFFE is also a closure, and it breaks the circular reference that would have been created via a reference to the outer lexical scope.


Avoid using a closure

JS Bin on jsbin.com

Depending on the use case, one might do away with the closure itself in order to avoid a circular reference. The closure has now been reduced to a simple onload function that calls another function. This method, ofcourse, dwells on a better understanding of closures, and what use cases justify the need for one.

Feel free to get back to me with any doubts or clarifications. I shall try my best to answer each one of them personally.



Receive my blogs in your email. Subscribe.

Only Javascript. No Spam. Promise.