Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for Global Execution Context to pop off the execution stack?

When JS code starts to run, the Global Execution Context is created and sits at the bottom of the execution stack as to "accomodate" Global Variable Object and'this'. If it is the case for the execution stack to get empty after the whole JS code is run and there is no Global Execution Context, how are we still able to access the global variables (for example, I am running an html file with its JS code and after its completion, I am still able to see the values of global variables through Chrome console...) or how this still points to global object (there shouldn't be any 'this' if there wasn't any Execution Context!)?

The only explanation I may give to myself is that Global Execution Context never leaves the execution stack; it is always there till I decide to close the browser window. Am I right or not?

Moreover, in case of asynchronous callbacks, when an event gets out of the event queue and gets into JS engine as to be run, what exactly happens in the execution stack? Is the callback's execution context sitting at the bottom of this stack or the global execution context is still there?

There is a similar subject Is the initial global execution context ever popped off the call stack in JavaScript?; however, it does not answer my questions.

Thank you

like image 896
Unknown developer Avatar asked Nov 23 '15 10:11

Unknown developer


People also ask

Is execution context call stack?

Execution stack, also known as “calling stack” in other programming languages, is a stack with a LIFO (Last in, First out) structure, which is used to store all the execution context created during the code execution.

What is global execution context?

Whenever the JavaScript engine receives a script file, it first creates a default Execution Context known as the Global Execution Context (GEC) . The GEC is the base/default Execution Context where all JavaScript code that is not inside of a function gets executed.

Is it possible to change functions context of execution?

If we want to, we can dynamically change the execution context of any method by using either call() or apply(). Both of these functions can be used to bind the "this" keyword to an explicit context.

What is the execution context & stack in JavaScript?

The Execution Context is an abstract environment created by JS to execute code. The Execution Stack is used to execute functions and keep track of all the Execution Context created. The Execution context is created in two stages, the creation and the execution stage.


1 Answers

The execution stack gets empty when the whole code is run.

how are we still able to access the global variables?

Even when no code is executed the global lexical environment with the global object still exists. When you feed some code into chrome console, the code is being evaluated, a new global execution context is being created and initialized with its lexical and variable environments set to the global environment and this bound to the global object. Then your code is executed within this context and the execution stack gets empty again.

how this still points to global object?

Every time a new global execution context is initialized with the global code, this gets bound to the global object.

in case of asynchronous callbacks, when an event gets out of the event queue and gets into JS engine as to be run, what exactly happens in the execution stack?

Again, a new global execution context is created and pushed onto the empty execution stack. In MDN this is described in slightly different terms than in ECMAScript spec:

When the stack is empty, a message is taken out of the queue and processed. The processing consists of calling the associated function (and thus creating an initial stack frame). The message processing ends when the stack becomes empty again. (MDN. Concurrency model and event loop)

Here "stack frame" means "execution context" and "initial stack frame" corresponds to "global execution context".

Is the callback's execution context sitting at the bottom of this stack or the global execution context is still there?

None of them. The stack is empty. And only if it is empty, the oldest callback is taken from the callback/event queue:

When there is no running execution context and the execution context stack is empty, the ECMAScript implementation removes the first PendingJob from a Job Queue and uses the information contained in it to create an execution context and starts execution of the associated Job abstract operation. ECMAScript 6.0 spec

like image 74
user2683246 Avatar answered Sep 17 '22 21:09

user2683246