Execution stack

  1. Before the global code is executed, the JS engine creates a stack to store and manage all the execution context pairs
  2. After the global execution context (Window) is determined, add it to the stack (pushdown)
  3. After the function execution context is created, add it to the stack (pushdown)
  4. After the current function completes, remove the object at the top of the stack (off the stack)
  5. When all the code has been executed, only Window is left on the stack
                    // 1. Enter the global execution context
    var a = 10
    var bar = function (x) {
      var b = 5
      foo(x + b)    // 3. Enter the foo execution context
    }
    var foo = function (y) {
      var c = 5
      console.log(a + c + y)
    }
    bar(10)         // 2. Enter the bar execution context
Copy the code

Execute the stack legend