Closure is often confused with Lexical Scope. Lexical Scope is an important part of closure, but it is not closure by itself. Let’s understand what a Lexical Scope is first.
Lexical scoping defines how variable names are resolved in nested fuction. For example, if we have child function within a parent function, then the child funtion can access to variables in child function as well as the parent function.
|
|
A Closure is a function that has access to the parent scope and preserves it, even after the parent function has closed.
|
|