Created
June 5, 2012 01:36
-
-
Save toddpi314/2871899 to your computer and use it in GitHub Desktop.
Closure Pinning Memory Leak - Fixed 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Component Definition | |
function Component(name) { | |
this.pageName = name; | |
this.onLoadedHandler = this.onLoadedHandler.bind(this); | |
} | |
// We can now do our work with the Component | |
// context passed as expected! | |
Component.prototype.onLoadedHandler = function() { | |
var otherPageName = this.pageName + 'hi!'; | |
} | |
// On Component.attach, lets subscribe to the | |
// DOM's load event and pass our context via the | |
// Javascript apply method (you can also use .call) | |
Component.prototype.attach = function() { | |
$(window).bind("load", this.onLoadedHandler); | |
} | |
// Detach the load event handler from the document | |
// context for-realz | |
Component.prototype.detach = function() { | |
$(window).unbind("load", this.onLoadedHandler); | |
} | |
// Test Case: Create a ton of Components | |
for (var i = 0; i <= 4; i++) { | |
var newComponent = new Component('my page'); | |
newComponent.attach(); | |
newComponent.detach(); | |
delete newComponent; | |
newComponent = null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment