Skip to content

Instantly share code, notes, and snippets.

@pimvdb
Created February 25, 2012 12:22
Show Gist options
  • Save pimvdb/1908200 to your computer and use it in GitHub Desktop.
Save pimvdb/1908200 to your computer and use it in GitHub Desktop.
Freeze variable in closure

To make closures inside loops a little more descriptive:

for(var i = 0; i < 5; i++) {
  freeze(i, function(i) {
    setTimeout(function() {
      console.log(i);
    }, 1000);
  });
}
function freeze() {
var args = Array.prototype.slice.call(arguments);
return args.pop().apply(this, args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment