This series has a lot of space to explain the context and scope, and some people reflect that these two are the same thing. This article uses a small example to illustrate that the scope and context are definitely not the same thing.
Before we start, let's summarize the differences in simple language.
00 Contextual Environment:
Can be understood as an invisible object (there are several attributes), although invisible, but it really exists, because all the variables are stored inside, otherwise we define where the variable is saved?
In addition, for a function, the context is created at the time of invocation, which is well understood. Take the argument as an example, you don't call the function, where do I know what parameters you want to pass me?
01 Scope:
First, it's very abstract. Second, remember the word: Except for global scopes, only functions can create scopes. Create a function to create a scope, whether you call not called, the function as long as it is created, it has a separate scope, there is a "site" of its own.
22 Persons:
A scope may contain several context environments. It is possible that there has never been a context (the function has never been called); it is possible that, now that the function has been called, the context is destroyed; it is possible that one or more (closures) exist at the same time.
The above text does not understand OK, and see the following example.
First, in addition to the global scope, each function creates a scope. The variables between scopes are independent of each other. Therefore, x in the global scope and in the scope of the FN, the two do not matter, do not affect each other, peaceful coexistence.
Second, the global context is generated before the program executes, and the variables are assigned values when the program executes.
Third, the program executes to line 17th, calling FN (5), which produces the FN (5) context, and presses the stack, and sets it to active state.
Finally, after the execution of Line 17th, the return value of FN (5) is assigned to F1. At this point the execution context is back to the global, but the context of FN (5) cannot be destroyed because there is a closure reference (which can be viewed in the previous article, which is not covered here).
Five, proceed to line 18th and call the FN function--fn (10) again. Generates the FN (5) context and presses the stack and sets it to the active state. At this point, however, the context of FN (5) is still in memory-there are two contexts in a single scope.
Here, the emphasis has been told, and then the scene here will not repeat.
The goal is to hope that we can use this example to clarify the context and scope of the relationship. Of course, it doesn't have to be like a college pie. The concept of a word, a simple understanding of the use of closures is helpful.
---------------------------------------------------------------------------
In-depth understanding of JavaScript prototypes and Closures (18)-Supplement: Context and scope relationships