Created
August 25, 2010 20:44
-
-
Save kinsteronline/550257 to your computer and use it in GitHub Desktop.
The classic javascript closure
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
/* | |
* 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