Closures are not hard to understand once you understand the core concept behind it. You won't understand it better if you read academic papers or anything like that so lets start by looking at some examples.
Most basic closure example
Here func1 creates local variable name and a function. the function doesn't have a local variable but reuses the variable defined in the parent function.
In the func2 there's a slight difference. Unlike in func1 here you return the displayName function. so in the displayName function definition gets assigned to the variable myFunc. But it also alerts 'Chrome' which was not a local variable to the displayName function.
You'll see Chrome in the alert is because displayName has become a closure. Closure is a special kind of object which combines
You must be careful when creating a closure inside a loop. Read more about it here at MDN article. Stackoverflow
Most basic closure example
Here func1 creates local variable name and a function. the function doesn't have a local variable but reuses the variable defined in the parent function.
In the func2 there's a slight difference. Unlike in func1 here you return the displayName function. so in the displayName function definition gets assigned to the variable myFunc. But it also alerts 'Chrome' which was not a local variable to the displayName function.
You'll see Chrome in the alert is because displayName has become a closure. Closure is a special kind of object which combines
- a function
- and the environment in which that function was created (environment consists of local variables that were in-scope at that time)
The environment consists of any local variable that were in-scope at the time that closure was created. It will be kept alive even after the function returns. In other words, whenever you see a function keyword within another function, the inner function has access to the variables in outer function. It's a stack-frame which is not de-allocated when the function returns. The local variables are not copied, they are kept by reference.
Closures can be used to different things. You can emulate private methods using closures (module pattern)
Closures in practice
Closures can be used to different things. You can emulate private methods using closures (module pattern)
Creating closures in loops
You must be careful when creating a closure inside a loop. Read more about it here at MDN article. Stackoverflow
Performance considerations
Creating closures will have some impact on the script performance from processing speed and memory consumption. When creating new object/class, methods should normally be associated to the object prototype rather than defined into the object constructor. This is because whenever constructor is called, the method will get reassigned for every object creation. (However redefining the prototype is not recommended).
Closure vs object performance (marijnhaverbeke)
Resolving this with Closures
sources:Closure vs object performance (marijnhaverbeke)
Resolving this with Closures