A closure is a pairing of: A function and A reference to that function's outer scope (lexical environment)
If a function was called by a function, which in turn was called by another function, then a chain of references to outer lexical environments is created. This chain is called the scope chain.
In the following code, inner forms a closure with the lexical environment of the execution context created when foo is invoked, closing over variable secret:
This box of the state is invisible to the caller of the function, delivering an excellent mechanism for data-hiding and encapsulation.
So, if you want a function to always have access to a private piece of state, you can use a closure. ...and frequently we do want to associate the state with a function. For example, in Java or C++, when you add a private instance variable and a method to a class, you are associating the state with functionality.
In this way, in the code above, secret remains available to the function object inner, after it has been returned from foo.
Uses of Closures
Private Instance Variables
In the following code, the function toString closes over the details of the car.
In the following code, the function inner closes over both fn and args.
In the following code, function onClick closes over variable BACKGROUND_COLOR.
In the following example, all the implementation details are hidden inside an immediately executed function expression.
The functions tick and toString close over the private state and functions they need to complete their work. Closures have enabled us to modularize and encapsulate our code.
This example shows that the local variables are not copied in the closure: the closure maintains a reference to the original variables themselves. It is as though the stack-frame stays alive in memory even after the outer function exits.
In the following code, three methods log, increment, and update all close over the same lexical environment.
And every time createObject is called, a new execution context (stack frame) is created and a completely new variable x, and a new set of functions (log etc.) are created, that close over this new variable.
In the following code, each time around the loop, a new function inner is created, which closes over i. But because var i is hoisted outside the loop, all of these inner functions close over the same variable, meaning that the final value of i (3) is printed, three times.
Returning a function from inside another function is the classic example of closure, because the state inside the outer function is implicitly available to the returned inner function, even after the outer function has completed execution.
Whenever you use eval() inside a function, a closure is used.
The text you eval can reference local variables of the function, and in the non-strict mode, you can even create new local variables by using eval('var foo = …'). When you use new Function(…) (the Function constructor) inside a function, it does not close over its lexical environment: it closes over the global context instead.
The new function cannot reference the local variables of the outer function.
A closure is created when a function is declared; this closure is used to configure the execution context when the function is invoked.
A new set of local variables is created every time a function is called.