Scope chain, closure, scope

⑴ Scope chain
Definition: When a function accesses a variable, it preferentially uses its own local variable. If there is no declaration of this variable, it will be accessed to the higher level until it reaches the global level. If there is no overall situation, the syntax error: is not defined.

⑵ Closure
definition: When the return value of a function is another function, and the returned function calls the internal variables of its parent function, and the returned function is executed outside, a closure is generated. Closure is an environment, specifically refers to external functions-higher-order functions

Features of closures:

①Function nested functions;
②Internal functions can directly access internal variables or parameters of external functions;
③Variables or parameters will not be recycled by garbage collection mechanism.

Advantages of closures:

①Variables reside in memory for a long time;
②Avoid the pollution of global variables;
③The existence of private members.

Disadvantages of closures: permanent memory, increasing memory usage, improper use will cause memory leaks.

⑶Scope:

Global scope: window.
Local scope: defined inside the function.

Guess you like

Origin blog.csdn.net/qq_45846359/article/details/109021097