What is a closure? How is it useful?

Hello everyone, I am Pu Lifan, a student of the 8th phase of the Chengdu Branch of the IT Cultivation Institute, an honest, pure and kind WEB front-end programmer.

Let me share with you today, what is a closure? How is it useful?


1. Background:

A closure occurs when a function can remember and access the lexical scope it is in, even if the function is executed outside the current lexical scope.


2. Knowledge analysis:

The concept of face is derived from "JavaScript You Don't Know", the variable a is defined in the scope of the function fn(), and the function fn() contains an internal function func(), the internal function func() holds A reference to the variable a. Under normal circumstances, when the function func is executed, the internal variables will be collected by the garbage collection mechanism (such as variable a). However, the function fn() returns the internal function func(), and the internal function func() will access the variable a at any time, so the garbage collection mechanism will not reclaim the internal scope of the function fn(), which is the meaning of the closure.

Example 1; function fn(){

var a = 100;

function func(){

console.log(a);

}

return func;

}

var func = fn ();

func(); //100

↑Assignment inside an external calling function;

Functions are called outside the lexical scope of the definition, and closures allow the function to continue to access the lexical scope at which it was defined.

definition:

Because the function can read the variables of the parent element, it cannot be reversed, and the functions of the same level cannot read the internal variables of each other, but the closure is different. Simply put, it is a function that can read the variables inside other functions.

As long as this situation is satisfied, it should be called a closure.



Closure effect: 1. You can use the scope of the same level

Closure function: 2. Read internal variables of other elements

Closures: 3. Extend the scope


What are the disadvantages of closures?

Disadvantage 1 of closures, scope is not so intuitive

Disadvantage 2 of closures, because variables will not be garbage collected, there is a certain memory footprint problem.



Question 1:

What happens if there is no var func = fn(); definition in the example/

Because this is a reference, if the function is removed, the function cannot form a closure, and it will be an undefined situation.

Question 2:

Why do you say you can access the scope of other functions?

Simply put, after the function closure, some scopes that cannot be accessed under normal circumstances can be completed, such as parent access to subsets and mutual access between siblings, making the function more flexible.

Question 3:

How can it be done without wasting memory?

You can use result=null to clear data that has not been garbage collected.



Author: Yi Xian_ab73
Link: https://www.jianshu.com/p/0e7b25339d92
Source: Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324764137&siteId=291194637