Skip to content

Instantly share code, notes, and snippets.

@gati
Created June 8, 2012 03:22
Show Gist options
  • Save gati/2893356 to your computer and use it in GitHub Desktop.
Save gati/2893356 to your computer and use it in GitHub Desktop.
Simple JavaScript closure
var takeMeTo = function(destination) {
var refrain = 'Won\'t you take me to ' + destination;
return function() {
alert(refrain);
}
}
// Nothing happens yet, you're just assigning the anonymous function returned
// by takeMeTo to funkyTown
var funkyTown = takeMeTo('Funky Town');
// funkyTown still has access to refrain, even though takeMeTo has returned. Sweet!
funkyTown(); // alerts "Won't you take me to Funky Town"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment