Submitted by Morris on Tue, 2006-02-21 10:19. Edited by the community since.
Closures are not magic
Degrees are Not hard to be understood as soon as the core concept is clarified. However, they are impossible to understand from reading theoretical or academically oriented explanations!
Two brief summaries
When a function () declares other functions (bar and baz), the family is the local variables created in not destroyed when the function is terminated. The variables only become invisible to the outside world. can therefore return the functions and cleverly and you can continue to read, write and communicate with each other via this closed family of variables ("the closure"). With whom nobody can interfere, not even someone who will call again in the future.
A degree is an opportunity to support top-notch features; It is an expression that can reference variables in its scope (when it was first declared), assigned to a variable, passed as an argument to a function, or returned as a function result.
An example of a degree
The following code returns a reference to a function:
In C and most other common languages there to a function back, all local variables can no longer be accessed because the stack frame is destroyed.
We can see from the output of that the code is referencing the variable. The anonymous function can refer to the one that contains the value because the local variables were kept alive by in a closure.
For some reason, closures seem really hard to understand when you read about them, but when you see some examples it becomes clear how they work (it took a while). I recommend working through the examples carefully until you understand how they work. If you started using closures without fully understanding how they work, you would be creating some very strange errors soon!
This example shows that the local variables are not copied - they are kept for reference. It is as if the stack frame is retained in memory even after the external function has ended!
All three global functions have a common reference to the conclusion same because they are all declared in a single call to.
The three functions share access to the same termination - the local variables of when the three functions were defined.
Tricky: Note that the variable is also located inside the closure and can be called from any other function that has been declared in, or that it can be accessed recursively from within the inside function.
This is a real problem for a lot of people so you need to understand it. Be very careful when defining a function inside a loop: the local variables from the closure may not behave as you might first think.
The line adds a reference to an anonymous function three times to the result array. If you are not that familiar with anonymous features, keep these things in mind:
Note that there are three logs when running the example! This is because, just like in the previous examples, there is only one termination for the local variables for (which,, and are). . When the anonymous functions on the line are called; They all use the same single deal and use the current value for and within that one deal (where has value because the loop had completed, and has value). Notice that we are indexing from 0, hence has the value. And the i ++ increases in value.
It can be helpful to see what happens when a declaration of the variables at block level (via the keyword) is used instead of a declaration of function-related variables via the keyword. When this change is made, each anonymous function in the array has its own closure. When the example is run, the output looks like this:
If the variable is also defined with instead of, the output is:
In this last example, a separate closure is created each time the main function is called.
When everything seems completely unclear, it is best to play with the examples. Reading an explanation is much more difficult than understanding examples. My explanations about closures, stacking frames, etc. are technically incorrect - they are gross simplifications to make it easier to understand. Once the basic idea is clarified, you can pick up the details later.
- Whenever you use in another function, a closure is used.
- Whenever you use in a function, a closure is used. The text you can refer to function local variables and you can even create new local variables by using in.
- If you use (the function constructor) in a function, no closure is created. (The new function cannot reference the local variables of the outer function.)
- It is probably best to think that a closure is only ever made one entry on a function and the local variables are added to that closure.
- Each time a function is called with a closure, a new set of local variables is retained (provided the function contains a function declaration and either a reference to that internal function is returned or an external reference is retained for it in some way).
- If you try to make dynamic source code changes (for example :) it won't work if there is a closure (of course you would never think of creating source code strings for substitution at runtime, but ...).
- It is possible to get function declarations within function declarations within functions ... and you can get closings at more than one level.
- I think usually a degree is a term for both the function and the variables that are being recorded. Note that I am not using that definition in this article!
The original article by Morris is in the Internet Archive.
- What is Renji's tattoo
- What is the access mode
- Does Lucifer exist Where does he live
- Why is true love important
- What are the uses of the leaf spring
- Is vitamin gelatin halal or not
- What do Croatians think of Operation Oluja?
- Quillette is a conservative publication
- What are the ratings for INIFD Pimpri
- How do you love the street dogs
- Are mobile towers built in residential areas
- What are your biggest discoveries in 2018
- How many years can officers work
- Is it unethical to follow up on email?
- What screams alcoholism
- Where do new drugs come from?
- How is Hawaiian BBQ made
- How does stress affect the eyes
- Why does the big bang theory end
- What is the source of citric acid
- Which wife of the president was an alcoholic?
- What is the best classic motorcycle
- What is rain of blood
- Are Israeli girls and Latinas similar