Execution stack
- Before the global code is executed, the JS engine creates a stack to store and manage all the execution context pairs
- After the global execution context (Window) is determined, add it to the stack (pushdown)
- After the function execution context is created, add it to the stack (pushdown)
- After the current function completes, remove the object at the top of the stack (off the stack)
- 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