Read full article on the Infragistics Blog
A JavaScript closure is a function which remembers the environment in which it was created. We can think of it as an object with one method and private variables. JavaScript closures are a special kind of object which contains the function and the local scope of the function with all the variables (environment) when the closure was created.
To understand closures, first we need to understand SCOPING in the JavaScript. We can create a variable or a function in three levels of scoping,
- Global Scope
- Function or local scope
- Lexical scope
I have written in details about scoping here, but let’s take a brief walkthrough of scoping before getting into closures.
Scopes in JavaScript
As soon as we create a variable, it is in a Global Scope. So, if we have created a variable which is not inside any function, it is in a global scope.
Leave a Reply