I have read 10s of SO references on closures, MDN references and other blog articles. They all seem to define closures in their own ways. For example, From MDN documentation:
function makeFunc() {
var name = "Mozilla";
function displayName() {
alert(name);
}
return displayName;
}
var myFunc = makeFunc();
myFunc();
And here goes their explanation of closure:
Normally, the local variables within a function only exist for the duration of that function's execution. Once makeFunc() has finished executing, it is reasonable to expect that the name variable will no longer be accessible. Since the code still works as expected, this is obviously not the case.
The solution to this puzzle is that myFunc has become a closure. A 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. In this case, myFunc is a closure that incorporates both the displayName function and the "Mozilla" string that existed when the closure was created.
This below StackOverflow post answers closures as a stack of visible scopes. What types of scope exist in Javascript?
Where I am confused: is closure an object? Or is it just a "anomalous scoping situation" where inner nested function has access to a variable defined outside of itself but local to the container parent function, even after parent function has already executed? Is closure an object referring to this nested function (scope) situation like myFunc
or is it the inner function itself?