The difference between scope and execution context

A difference:

  • The global execution context is created immediately before the JS code executes after the global scope is determined [1].
  • The function execution context is when a function is called (fn()), created before the function body code is executed.

Difference between 2:

  • In addition to the global scope, each function creates its own function scope,The scope is defined when the function is defined, rather than when the function is called
  • The scope is static, existing as long as the function is defined and does not change.
  • The execution context is dynamic, and when a function is called (fn()), automatically released after the call ends

contact

  1. The context (object) is subordinate to its scope.
  2. The global context (object) corresponds to the global scope.
  3. The function context (object) corresponds to the function scope.

Execute the context life cycle

  1. Creation Phase [1]
    • Generating variable objects
      • Create the arguments
        • Scanning function declaration
        • Scanning variable declaration
    • Simultaneous scope chains
    • Make sure this points to
  2. Execution phase
    • Variable assignment
    • Function reference
    • Execute other code