Skip to content

Instantly share code, notes, and snippets.

@wolfieorama
Last active May 23, 2016 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfieorama/eeebd244691e5d04a4baa0417100efd5 to your computer and use it in GitHub Desktop.
Save wolfieorama/eeebd244691e5d04a4baa0417100efd5 to your computer and use it in GitHub Desktop.
function testClosure (){
var x = 4;
function closeX(){
return x; // NB: x is not stored anywhere in the innermost function its a global variable
}
return closeX;
}
var checkLocalX = testClosure();
checkLocalX(); // this will return 4 even testClosure function has been closed because its local variable has now been bound within checkLocalX
// because it has been bound as a closure within closeX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment