Javascript Hints - #3 Closure and memory leakage

Hello there! A bring some memory leaks in your home. Don't blame me for it.

function execute = function(name) {
   var test_object = createSomeBigObject();

   return function() {
      alert('Hello, ' + name);
   }
}

var result = execute('Andrew');

Although test_object doesn't used in returned function, we still have reference on it. Of course, till result object is alive.


Yet another example:

function attach() {
  var element = document.getElementById("my-element");

  element.attachEventListener("click", function() { 
     alert("Clicked: " + element.innerHTML); 
  });
}

  • Slider

You may also like

  • Slider
  • Slider
2 comments

John Doe

Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non | numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.

Reply