function a(){
b();
var c;
}
function b(){
var d;
}
a();
var d;
I would like a clarification on Execution Context for the code above. From what I understand, during the creation phase of Execution Context functions a and b are set as pointers to the location in heap memory and var d is set to undefined. During the execution phase, function declarations a and b are simply ignored.
What I'm confused about is when we invoke function a during execution phase, is Global Execution Context is still in execution phase later when we pop a()'s execution context from a stack, so we can process var d? Or is GEC's execution phase is over once we invoke a() and then we somehow scan the var d when GEC is the only context left on a stack?
From what I understand, after GEC execution phase, a() will be invoked and new a() execution context will be put on execution stack. Then after a()'s execution phase is done, we put new b() execution context on a stack. After we pop b()'s execution context, we can process var c and after we pop a()'s execution stack we can process var d of global execution stack.
The biggest confusion is how JS engine checks var c and var b if execution phase for both contexts is already over. Is execution phase for previous context actually over or is it still running for each context? Are we able to scan var c and var d due to a Variable Object(VO) saving information about current execution context or due to all previous execution contexts still running an execution phase? I do not understand whether execution phase for given context is finished on function invocation of a next context or when we pop a current/given context from a stack.
So, the callstack in js works on LIFO, considering your example the execution context will be executed in following manner
[gec]
[a execution context]
[gec]
[b execution context]
[a execution context]
[gec]
[a execution context]
[gec]
[gec]
I hope this helps you!!!