Now if you combine all those functions (not one, but all) with each of their lexical environment, that is called Closure!
Explain this in the code please.
If you see the code mentioned in this post on the top, that itself is a closure(the function b has access to var num that is outside the function b, which is also its lexical environment). What I meant by "combine all those functions (not one, but all)" is that, if you had many nested functions here like:
functiona() {
var num = 10;
functionb() {
var newNum = num + 5;
console.log(newNum)
functionc() {
console.log(num)
functiond() {
console.log(num)
}
}
}
}
each of these functions has its own lexical environment, right? And this whole code is called closure[ combine all the lexical environments, you get a closure]. As it is closure, all these functions will have access to var num, and also any variables declared in the global scope.
I think that last line in my post is confusing. Will update it.
Which one is a closure? function a or function b or function c or function d ? Which one ? If not function then from which part to which part, I should say that it is closure? Mansoor Ameen