Invocation – Running a function. Calling a function. Invoke a function.
We do this by using ‘()’ behind a function reference.
1 2 3 4 5 6 7 8 9 |
function b() { } function a() { b(); } a(); |
When source is run, first
Global Execution Context (this, global obj, attach functions to this, hoisting everything)
In our example, the JS compiler will parse the code and put our functions an and b into memory.
In the Execution Phase, we hit a() first. At this point, an execution context for a() is created and placed on the execution stack.
Note that whichever context is at the top, is the current one running.
It will have its own memory space for variables and functions.
It will go through its own Create/Execution Phase.
Then in a()’s execution phase, it hits b().
An execution context for b() is created and placed onto the execution stack.
This is how function invocation happens in JS.
Every function creates a new execution context, and goes through its Create/Execution phase.
When b() finishes, it gets popped. Then a() gets popped. And back to the global.