What exactly does closing mean?

In conjunction with closures, I often read that something closes on top of something else as a means of explaining closures.

Now I have no particular difficulty in understanding closure, but "closure" seems to be a more fundamental concept. Otherwise, one could not refer to this to explain the closure, would it be?

What is the exact definition of closure, but something and something else? Where does this term come from?

+6
source share
1 answer

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.

+8
source

Source: https://habr.com/ru/post/978582/


All Articles