The scope chain depends on the position when the function is declared. When the identifier is parsed, the current scope is first searched and then outward until the global order is reached. It is irrelevant to where the function is called; the concepts of scope, scope chain, execution environment, execution environment stack, and this are very important in javascript. I often confuse them here;
The region inside the local scope function. The global scope is window;
The scope chain depends on the position when the function is declared. When the identifier is parsed, the current scope is first searched and then outward until the global order is reached. It is irrelevant to where the function is called;
The execution environment is the set of data and variables accessible to the function, that is, all data and variables in the function scope chain;
The execution environment stack is based on the Code Execution sequence. Each execution environment is accessed layer by layer in the form of stacks, And the execution environment is discarded when it is used up and exited; if no variable is found in the current execution environment (storing data and variables in the current scope chain), the variable cannot be found and will not be searched in the previous execution environment, it is different from the scope chain;
Scope
JavaScript does not have block-level scopes, but only function-level scopes: variables are visible in the declared function bodies and their subfunctions.
The scope is the accessible range of variables and functions. It controls the visibility and lifecycle of variables and functions. In JavaScript, the scope of variables includes global and local scopes.