Closures

Definition

Closure has to do with scope.

Functions in Javascript create their own scope. Variables can be declared inside of a functions scope, these are not accessible in the parent scope.

Functions can however access variable in their parent scopes.

Functions in Javascript are first class, which means they can be passed in to other functions, as well as returned from other functions.

In order for Javascript to do this, the functions maintain a live link to variables they are making use of from where the function is defined.

When a function makes use of a variable which is not inside its own scope, it forms a closure over the variable. This means it maintains a live link to the current state of the variable.

How closure is commonly used