Scope description: Generally understood as the scope of a variable

Global scope:

  1. Global action clauses are created when the page is open and destroyed when the page is closed.
  2. Writing in theThe script tagVariables and functions that are globally scoped and accessible anywhere on the page;
  3. There are in the global scopeGlobal object WindowRepresents a browser window that is created by the browser and can be called directly;
  4. Variables and functions declared in the global scope are treated asThe window objectProperties and methods of

Function scope:

  1. When a function is called, the function scope is created, and when the function is executed, the function scope is destroyed.
  2. Each call to a function creates a new function scope, which is independent of each other.
  3. Variables in the function scope can be accessed globally, but variables in the function scope cannot be accessed outside the function scope.
  4. When a variable/function is accessed in a function scope, it is searched in its own scope first. If it is not found, it is searched in the function’s upper scope until it reaches the global scope.

A deeper understanding of scope

  • The context of the executor
    • An internal executor context is created early in the execution of the function codeObject AO(scope);
    • This is created when the internal object is precompiled, because the function is precompiled when called;
    • An execution-time context is created at the beginning of global code executionObject the GO.
    • JS precompilation is also briefly mentioned here

Function scope precompiled

  1. Create AO object AO{};
  2. Find the parameter and variable declarations and treat the variable and parameter names as the property names of the AO object, with a value ofundefined;
  3. The parameters are unified;
  4. Find the function declaration inside the function body and assign the value to the function body

Precompilation of global scope

  1. Create the GO object
  2. Find the variable declaration and use the variable name as the property name of the GO object, with a value ofundefined
  3. Find the function declaration and assign the value to the function body