3 ms·
Eek! Number 4, misuse of closures refactored the code into something equivalent, but both have circular reference memory leak, bad form of the author to create
by tmp123456 16y ago
Eek!
Number 4, misuse of closures refactored the code into something equivalent, but both have circular reference memory leak, bad form of the author to create an example of something with other bad code in the snippet.
A much nicer answer would be to refactor into the non-leaking version, which was very straightforward from the first snippet.
function createAlert(a, b, c, d) {
return function() {
alert (a + b + c + d);
}
}
function(a, b, c) {
var d = 10;
var element = document.getElementById(‘myID’);
element.onclick = createAlert(a, b, c, d);
}
This will no longer leak and is much easier to read.
The author tries to give an example of a non-leaking version further down, but unfortunately tryies to delete a variable, which is not allowed in strict mode.
delete element; // bad
a much nicer version is to delete the callback from the element instead (from within the callback closure itself).
delete element.onclick;
(or just use the non-leaking version in my comment in the first place).
Couldn't see any other problems on quick glance.