ADVANCE FUNCTIONS JS
A function has access to the variable environment (VE) of excecution context it was created
Closure: VE attached to the function, exactly as it was at the time and place the function was created
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
A closure makes sure that a function doesn't loose connection to variable that existed at the function's birth place.
A closure is like a backpack that a function carries around wherever it goes. This backpack has all the variables that were present in the environment where the function was created.
We do NOT have to manually create closures, this is js feature that happens automatically. we can't even access closed-over variables explicity. A closure is NOT a tangible JS object
Comments
Post a Comment