Skip to content

Instantly share code, notes, and snippets.

@wolfieorama
Last active May 23, 2016 18: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/174d7e3d8eae48c931c6548fc9a6dd25 to your computer and use it in GitHub Desktop.
Save wolfieorama/174d7e3d8eae48c931c6548fc9a6dd25 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 if testClosure function has been closed, this is 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