Closures generally happen as a result of writing code that relies on lecical scope.
Closure is when a function can remember ancd access its lecical scope even when it is invoked outside its lexical scope.
It basically allows functions to continue to access the lexical scope it was defined in at author-time.
let keyword in a for loop instead of
var for declarign the
"i" variable, block scope and closure work hand in hand to circumnavigate any semantic errors that would have been encountered otherwise.
Modules also leverage the power of closures.
For the module pattern to be exercised:
The notes below appear in the numerous appendicies that are contained in the book after the final chapter.
Lexical scope is write-time, while dynamic scope is runtime.
What this implies is that in lexical scope is statically defined by the author at wirte time, while dynamic doesn’t care which scope it was written in, it on concerns itself with which ‘resources’ as closest to it at runtime – think call stack.
The concept of Lexical
this was introduced in ES6, by fat arrow functions.
this takes on the
this value of their immediate lexical enclosing scope, whatever that scope is.