admin管理员组

文章数量:1297002

When JS code starts to run, the Global Execution Context is created and sits at the bottom of the execution stack as to "acodate" 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 pletion, 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

When JS code starts to run, the Global Execution Context is created and sits at the bottom of the execution stack as to "acodate" 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 pletion, 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

Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Nov 23, 2015 at 10:46 Unknown developerUnknown developer 5,97017 gold badges60 silver badges118 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15 +50

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 bees 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

本文标签: javascriptIs it possible for Global Execution Context to pop off the execution stackStack Overflow