Closures in JavaScript

This post is continuation of the series of posts on Modular approach in JavaScript

A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression).

In JavaScript "closure" is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consists of any local variables that were in-scope at the time that the closure was created.

Example 1:

Example 2:

Comments