About JS Closures

Source: Internet
Author: User

About JS Closures
Excerpt from Wu Liupe, the original JavaScript closure! 》
First, related concepts
1. Execution context
The runtime environment when executing the JS code is called the execution context. Includes: Global code-The default environment for the first execution of code, function code-whenever the execution process enters the function body.
The execution context is the execution environment or scope of the current code.
2. Execution process
Global: After starting a program, the variables declared in the global execution context are global variables, starting from the global execution contexts
function Body: When invoking a function, JS creates a local execution context-The local execution context has its own set of variables-the local variables of the function-the new execution environment is thrown onto the execution stack.
(The execution stack is the mechanism used to track the execution location of the program)
function encounters a return or end symbol: The local execution context jumps out of the execution stack--the function sends the return value to the calling context (the calling context is the execution context that called the function,
Can be either a global execution context or another local execution context)--The local execution context is destroyed (local variables are destroyed)
3. Lexical scopes
Variable lookup procedure: Local execution context (local variable)-Invoke context (local variable/global variable)-Global context (global variable)
A function can access a variable defined in its invocation context, which is called a lexical scope.
4. Closure principle
When declaring a function, it contains a function definition and a closure. A closure is a variable that is declared by a function creation.

        eg:function createFn(){            let counter = 0;            const myFn = function(){                counter = counter + 1;                return counter;            }            return myFn;          }    全局范围中创建的函数也会创建一个闭包,但由于其是全局的,可以访问全局范围内所有变量,就无所谓闭包不闭包了。    *当一个函数返回另一个函数,并使用其调用上下文中变量时,才会真正涉及闭包。返回的函数可以访问仅存在于其闭包中的变量。    当创建和传递一个函数或将其从另一个函数返回时,这个函数就带有一个背包,背包中包含了所有在创建函数时声明的变量。

Second, the understanding of closures
Closures: A mechanism for reusing local variables and protecting the local variables from contamination. (It has the advantage of reusable global variables, and avoids the disadvantage of global variables being susceptible to pollution)
Use:
Define an outer function: Define a protected local variable (that is, a closure)--Returns an intrinsic function object that specializes in manipulating local variables
Call the outer function to get the returned intrinsic function object
Operation of protected local variables-unique paths using the returned intrinsic function object
Conclusion: Multiple inner function objects returned by the same outer function call share a protected variable
The inner layer function returned by the outer function call two times, using each protected copy of the variable independently.

About JS Closures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.