I have read another question on stackoverflow, he said:
- A closure is one way of supporting first-class functions; it is an expression that can reference variables within its scope (when it was first declared), be assigned to a variable, be passed as an argument to a function, or be returned as a function result.
- Or, a closure is a stack frame which is allocated when a function starts its execution, and not freed after the function returns (as if a 'stack frame' were allocated on the heap rather than the stack!).
But I have also read MDN, it said:
A closure is the combination of a function and the lexical environment within which that function was declared.
I think they are totally different. But if MDN is right,
var a=1;
function printA(){
console.log(a);
}
this code include a function printA() and its lexical environment(variable a), does it mean this code is closure? Or MDN is wrong?