Variable ascension

Function test(){console.log('test')} function test(){console.log('test')} Var test1 = function(){console.log('test')}Copy the code

Js code compiles and creates the execution context

  • Global execution context (only one within the life of the page)
  • Function call, execution context
  • With eval, the code is compiled and the execution context is created

Stack: last in, first out

Call stack (execution context stack) : Manages the execution context. The JS engine pushes the execution context onto the stack and pops the execution context off the stack after execution

var a = 3
function test(b, c) {
    var d = 1
    return add(b, c)
}

function add(b, c) {
    return b * c
}

test(4, 5)
Copy the code

Console.trace () prints the current function call relationship

Stack overflow: Call stack space used up (recursion without termination conditions)

Can a timer be added to solve stack overflow?

Global functions like setTimeout and setInterval Promise are not part of JS, but part of WebAPI. When a webApi is encountered, its callback function is handed over to the Web apis. At this point, function Foo in the call stack completes and exits the stack, leaving the stack empty. The callback function is sent to the task queue and waits for the Event loop to scoop it up and put it back on the stack

Reference: juejin. Cn/post / 684490…