Cannot say now baidu or the answer of Google is wrong or what, however when you see the fast food above the network cannot be solved when the individual feels actually go to the book to see meeting should have one time receive goods, good go up dry goods

A function that has access to a variable in the scope of another function. A function that has access to a variable in the scope of another function. A function that has access to a variable in the scope of another function. Function ???? Principle: The first thing you need to understand is a east-west scope chain and before you can understand this you need to understand some other necessary concepts: the execution environment (which we now refer to simply as the environment) : the execution environment defines a variable or other data that a function has access to and each environment has an associated variable object, Variable objects store variables and functions in the environment. For example, according to my understanding, they give each person a scope of authority, similar to the emperor and the hundred officials. For example, when you use JS, you often hear a sentence that in the browser the global environment is considered the Window object

Scope chain: when accessing a variable in the current execution environment, if it does not exist, it will search for the outer layer, and finally find the outermost layer, which is the global scope. So you have a chain closure where one function is wrapped around another function, or another way of saying it is a function that has at least one function wrapped around it, and that function can access the value of the variable object in the context of the outer function, so here’s a summary

Closure action 1: Hides variables to avoid global contamination

Function 2: Reads variables inside a function

At the same time, when closures are not used properly, advantages become disadvantages:

Disadvantage 1: Variables cannot be collected by the garbage collection mechanism, resulting in memory consumption

Disadvantage 2: Improper use of closures can cause memory leaks

A brief explanation of why variables are not destroyed by the garbage collection mechanism when using closures is given here.

JS specifies that within the scope of a function, variables will be destroyed after the execution of the program, which can save memory;

When using a closure, by the nature of the scope chain, variables outside the closure (function) are not destroyed, because the function is always called, so there will always be, if the closure is used too much will cause memory destruction.