Skip to content

Instantly share code, notes, and snippets.

@esfand
Created April 7, 2014 13:07
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 esfand/10019950 to your computer and use it in GitHub Desktop.
Save esfand/10019950 to your computer and use it in GitHub Desktop.
Javascript Closure

A closure is the set of local variables that are kept alive after a function call* has completed, because those variables may still be required by a local function, and a reference to a local function has somehow been exported during the function call.

The local function can be returned by the enclosing function, that’s one way of exporting its reference, but there are other common ways too, the local function can be attached to an event, pushed onto an array, anything to prevent it from being dereferenced once the enclosing function has completed.

  • doesn’t have to be functions, it can be any execution context, but let’s just leave it at function calls for now, shall we?

A closure is effectively all the stuff that belongs to an expired execution context that cannot yet be discarded because there are still references to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment