From the wiki article, a closure (computer science) is a function or reference to a function together with a referencing environment-- a table storing a reference to each of the non-local variables of that function.
Closure-like constructs include callbacks and as such, are important in asynchronous programming. Here is a simple example in PHP that uses a closure as a callback to compute the total price of a shopping cart by defining a reference table for the callback function and including variables tax and total:
- Functions can refer to variables defined in outer functions even after those outer functions have returned
- Closures can update values of outer variables
Knowing this, we can do some fun stuff in Node.JS with asynchronous programming. With closures, we can pull a document collection from a NoSQL database, manipulate the results, and push it to an array stored via closure in the parent scope.