What Is a 'Closure'?
I Asked a Question About Currying and Closures Were Mentioned. What Is a Closure? How Does It Relate to Currying? 5 24 Answers Variable Scope When You Declare...
I asked a question about Currying and closures were mentioned. What is a closure? How does it relate to currying?
24 Answers
Variable scope
When you declare a local variable, that variable has a scope. Generally, local variables exist only within the block or function in which you declare them.
function() {
var a = 1;
console.log(a); // works
}
console.log(a); // fails
If I try to access a local variable, most languages will look for it in the current scope, then up through the parent scopes until they reach the root scope.
var a = 1;
function() {
console.log(a); // works
}
console.log(a); // works
When a block or function is done with, its local variables are no longer needed and are usually blown out of memory.
This is how we normally expect things to work.