Consider:
something closes over something else |_______| |_________| |____________| | | | subject verb object
Here:
- The object is closing. Closing is a function.
- Closing โclosesโ (as an attachment) the set of its free variables.
- An object is a set of free closure variables.
Consider a simple function:
function add(x) { return function closure(y) { return x + y; }; }
Here:
- A function named
add has only one variable named x , which is not a free variable, since it is defined within add itself. - A function named
closure has two variables named x and y , of which x is a free variable since it is defined in the scope of add (not closure ) and y not a free variable because it is defined in the scope of closure .
Therefore, in the second case, a function called closure is called a "close" variable called x .
In this way:
- A function called
closure is called a closure of a variable called x . - A variable named
x is called an upvalue function called closure .
That is all that is needed.
source share