This is the fifth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
closure
- Closures are an advanced use of our functions
- Before we talk about closures we need to review functions
Two stages of a function
-
We’ve always said that a function has two stages
- Definition phase
- Call stage
Function definition stage
- Create a storage space
- Put the code in the body of the function exactly in this space (without parsing variables)
- Give the address of the storage space to the function name
Function call phase
- Find the storage space of the function by the address of the function name
- Parameter value
- Preliminary analysis
- Take the code out of the function storage space and execute it.
Redefine the function call phase
-
Find the storage space of the function by the address of the function name
-
Parameter value
-
Preliminary analysis
-
Create an execution space in memory
-
Take the code out of the function storage space and execute it in the newly created execution space
-
After the execution, the execution space in the memory is destroyed
Function fn() {console.log(' I am fn function ')} fn()Copy the code
- When the function executes, it creates onePerform spaceWe’ll call him
xxff00
) Console. log(' I am the fn function ')
This code is inxxff00
This space- After the code is executed, this
xxff00
The space is destroyed
- When the function executes, it creates onePerform spaceWe’ll call him
Function execution space
-
Each function will have one storage space
-
But each call generates a completely different execution space
-
And the execution space is destroyed after the function is executed, but the storage space is not
-
So what’s the point of this function space being destroyed when it’s finished executing?
- There are ways we can keep this space from being destroyed
- The closure is to take advantage of this undestroyed execution space
Function execution space is not destroyed
-
The execution space of a function is destroyed after the function is completed
-
However, once a reference data type is returned inside the function and a variable is accepted outside the function
-
Then the function execution space will not be destroyed
Function fn() {const obj = {name: 'Jack', age: 18, gender: 'male'} return obj} const o = fn()Copy the code
-
When a function executes, it generates a function execution space (let’s call it xxFF00).
-
The code executes in the XXFF00 space
-
Name an object space in the space xxFF00 (XXFF11)
-
The xxFF11 object address is returned in the execution space xxFF00
-
The zero outside of the function takes the address of an object, right
- But it’s one in
xxff00
Function execution spacexxff11
Address of the object - because
o
The variable is always associated with the object address, soxxff00
This space will never be destroyed
- But it’s one in
-
Wait until when to execute a code o = null
- At this point,
o
Variables are more than correlated inxxff00
Function execution spacexxff11
Address of the object - So, this time the function executes space
xxff00
Is destroyed
- At this point,
-
closure
- Closures take advantage of this function to execute space without destroying logic
- Several conditions make up a closure
Not destroyed space
-
The first condition of a closure is that it utilizes the logic of not destroying space
-
It just doesn’t return an object data type
-
Instead, return a function data type
function fn() { return function () {} } const f = fn() Copy the code
f
The variable accepts oneFn execution spaceThe function in
Internal functions refer to variables in external functions
-
There are two functions involved
-
The inner function looks at or uses the variables of the outer function
Return function a() {console.log(num)}} const f = fn()Copy the code
fn()
It will generate onexxff00
Execution space of- again
xxff00
Inside this execution space, we define aa
Function of theThe storage spacexxff11
- The global f variable accepts phi
xxff00
The inside of thexxff11
- so
xxff00
It’s the space that won’t be destroyed - because
xxff00
It will not be destroyed, so the num variable defined inside will not be destroyed either - In the future
f()
You can access the num variable
Characteristics of closures
-
Why is it called a feature, because each of its points is a strength and a weakness
-
Scope space is not destroyed
- Advantages: Because not destroyed, the variable page will not be destroyed, increasing the life cycle of the variable
- Disadvantages: because do not destroy, will always take up memory, more later will lead to memory overflow
-
You can use closures to access variables inside a function that are outside of another function
- Advantage: Internal data can be accessed outside the function
- Disadvantages: The reference must always be held so that the function execution stack is not destroyed
-
Protecting private variables
- Advantage: you can put some variables inside a function without polluting the global
- Disadvantages: Use closure functions to access, not very convenient
-
Closure concept
- You have A function A, and inside A returns A function B
- And then there’s A variable outside of A that references this function B
- Function B accesses private variables inside function A
- The above three conditions are indispensable