js magical closure

The definition of closure
insert image description here
is what Baidu Encyclopedia says. Closures are functions that can read variables inside other functions. For example, in javascript, only sub-functions inside a function can read local variables, so closures can be understood as "functions defined inside a function". In essence, a closure is a bridge that connects the inside of a function with the outside of a function.
Let's look at a simple example

function d(){
    
    
    var b = 11;
    function v(){
    
    
        return b++
    }
    return v
}

This is a common and simple closure, where the child function reads the local variables inside the parent function.
Let's look at another example

(function(){
    
    
	let d = 22;
	function b(){
    
    
		return	d++
	}
})

Is this a closure?
This is also a closure and still satisfies the definition of a closure.
When we usually write code, we actually use closures inadvertently.
Summarize two points;
1. Function nesting.
2. Access the internal variables of other functions.
Disadvantages of closures.
It is easy to pollute memory.
This is very important to say the least.
When we use closures, if the function is a function that needs to be used all the time, then there will be no memory pollution.
But if it is some closures that are used inadvertently and are not used multiple times. That creates pollution.
So be good at observation, and if you find this situation, just clear the variables.

-------------------------------------------Manual segmentation Austrian
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Later, I read the explanation of closures in js that you don’t know. It’s still the
same as what I said before. This closure exists all the time in the code we write, but we didn’t recognize it as a closure.
look at the code

function wait(message) {
    
    
 setTimeout( function timer() {
    
    
 	console.log( message );
 }, 1000 );
}
wait( "Hello, closure!" );

Looking at the above code, if we look at it with the concept of closure, it is difficult for us to see that this is a closure. So looking at it from another angle,
functions (with access to their respective lexical scopes) are treated as first-level value types and passed around.
Is this easy to understand?
Pass an internal function (named timer) to setTimeout(...). timer has
a closure that covers the scope of wait(…) and thus also holds a reference to the variable message.
After wait(…) is executed for 1000 milliseconds, its internal scope will not disappear, and the timer function still retains the
closure of the wait(…) scope.
That's what it says in js that I don't know about.
If you pass functions (with access to their respective lexical scope) as first-level value types and pass them around,
you'll see closures applied to those functions. In timers, event listeners, Ajax requests, cross-window communication, Web Workers or any other asynchronous (or synchronous) task, whenever you use a callback function, you are actually using a closure!
Here let us look at closures from another angle,

It's not clear, I hope the boss can give me some pointers.

Guess you like

Origin blog.csdn.net/weixin_44655037/article/details/110952934