The function of closure and its advantages and disadvantages

Time:2019-11-10

In interview questions, closure should be a must ask question (the following content is purely personal understanding, welcome to correct the lack)

Let’s take a brief look at what a closure is

 

First of all, let’s talk about closures

In short, it is a function defined in the function, which can read other functions’ internal variables. In essence, closure is a bridge connecting the internal and external functions

 

So why do we use closures to read variables inside functions, or what are the functions of closures?

This involves the life cycle of variables. The variables defined within a function belong to local variables. The life cycle of local variables is: when the function it belongs to is called, it starts. Once the call execution is finished, the local variables will be released. When we need the internal variables of a function, they have been released and can’t be read. How to solve this problem Decide? We need to find a way to extend his life cycle

The purpose of a closure is to extend the life cycle of a local variable. After the function is executed, the local variable cannot be released by memory, and then the external can access the variable

 

How can we judge whether a closure exists

Closures have the following characteristics:

1. Function nested function, closure must have nested function

This meaning can be roughly deduced from the literal definition of a closure, which is a function defined within a function;

So why do we have to nest functions? Because local variables are needed, the purpose of closure is to access the local variables inside the function. If we do not define local variables, our goal will not be achieved — to extend the life cycle of variables

2. The outer function must have local variables, and the inner function must operate this variable of the outer function

3. The outer function must return the inner function to the outer, and use return

Why use return to return the inner function? Because if you don’t return the inner function, you can’t use the closure. The ultimate purpose of returning the inner function is to let the external access the closure. If the external can’t access the closure, how can the closure bridge the internal and external communication of the function

 

When we do closure, we also need to pay attention to the following two points:

1. When the outer function is called multiple times, a new scope will be created, that is, the local variables of the outer function operated by the inner function will not be affected

In short, this is the first time I call this outer function, I use the internal local variable p; when I call this outer function the second time and the third time I use the variable p, I will create a new scope, which will not affect the previous call

2. If the inner function returned by the outer function is called several times, the local variables of the outer function operated by the inner function will change several times

The above two points can be seen through the following code

  

 

 

 

Here are the specific output results

 

  

 

You can combine the code and output results to analyze what you didn’t understand,

The first point: the output of each outer function call does not affect each other. It can be seen that the first output of each call is 0(The outer function is called here);

Second, from the output results of my [one first call], [two second call], [three third call], we can see that I output every time(Inner function called once), the value of the variable will change, increasing by 1. I output several times, that is, I call several times, and the variable will change several times

Please note that although all of the above are called, the function called is different. The outer function is called, and the variables do not affect each other. The inner function is called, so the variable changes according to the number of times you call

I don’t know if you can understand what I want to express here

 

 

 Disadvantages of closure

 

Closures prevent variables inside functions from being released by memory. These variables will occupy memory and consume a lot of memory, which may lead to memory leakage

The solution to this problem is to delete all unnecessary local variables in time when these variables are not used

 

 

 

 

  

 

Recommended Today

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]