1. Closures and anonymous functions are two different concepts
Closures are functions that have access to variables within the scope of another function
2, create the form of closures:
Create another function inside a function
Understanding closures:
What happens to the function call procedure:
(1) When a function is called, an execution environment and the corresponding scope chain are created.
(2) Initializes the active object of the function with the values of arguments and other named parameters.
In the scope chain, the active object of the outer function is always second, and the outer function of the external functions of the active object is in third place .... , up to the global execution environment where the scope chain is focused
The above code first defines the compare () function and then calls it in the global scope. The call to Cpmpare () creates an active object that contains arguments, value1, and value2. Variable objects for the global execution environment (containing result, and compare) are second in the scope chain of the Compare () execution Environment
In the above example, the global activity object has: Compare, result, compare () Active objects are: value1, value2, arguments
JS Closed Package