Skip to content

Instantly share code, notes, and snippets.

@kinsteronline
Created August 25, 2010 20:44
Show Gist options
  • Save kinsteronline/550257 to your computer and use it in GitHub Desktop.
Save kinsteronline/550257 to your computer and use it in GitHub Desktop.
The classic javascript closure
/*
* When I get frustrated with looking a javascript that
* looks far more like java, I really like looking at this
* code to make me a bit happier.
*
* http://en.wikipedia.org/wiki/Closure_%28computer_science%29
*/
var printMessage = function (s) {
var f = function () {
console.log(s + ' was pressed.');
}
return f;
}
buttonA.onClick = printMessage('A');
buttonB.onClick = printMessage('B');
buttonC.onClick = printMessage('C');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment