In JavaScript, closure refers to a function “remembering” the variables outside of it.
This behavior is exclusive to returned functions. Let’s have a look:
function whatDay(weekDay) {
return function saveDay() {
console.log(weekDay);
}
}
let today = whatDay("Saturday")
today() // Saturday
whatDay
is a function that returns a function saveDay
.today
a function call of whatDay
with "Saturday"
as the argument for whatDay
's weekDay
parameter.saveDay
now closes over the outer variable weekDay
(whose value is "Saturday"
).today
is now permanently linked to the variable, even after it has "expired" in the outer whatDay
function after its life cycle completes within the function's block. …Often, we don’t say what we think; more often, we don’t write what we believe. The medium is the message.
Sometimes, however, we take the time to think about things, only to find ourselves ensnared by semantics. In the same vein, the minds of many people appear to be possessed, or otherwise entangled, by ideas and thoughts which appear meaningless when we think about them.
We, as a species, are in the habit of inventing artificial constructs, realizations of concepts, to name things, thereby divorcing them from their natural nameless forms. To name something is to guillotine that thing. Our minds have limited capacity, so we must make do with names and labels. …
About