-1

In this tutorial https://hackernoon.com/execution-context-in-javascript-319dd72e8e2c. In the order of writing code, the invoking of functionA() precede console.log("GlobalContext"). However, in the execution context stack, the order is reversed. I wonder, is it right that the Global Execution Stack always get higher priority and execute first? Execution Context Stack

Binh Le
  • 353
  • 3
  • 11
  • 1
    Possible duplicate of [Why let and var bindings behave differently using setTimeout function?](https://stackoverflow.com/questions/31285911/why-let-and-var-bindings-behave-differently-using-settimeout-function) – 31piy May 07 '18 at 03:10

1 Answers1

0

You may be interpreting the picture on the right upside-down.

A common model for software stacks used in computing is to initialize a stack point to a memory address known as the "bottom of the stack". The stack pointer is adjusted to increase the stack size (the offset of the SP from the bottom of the stack) as stack frames and stack frame items are pushed onto the stack. Conversely stack size is reduced as items are removed from the stack.

The picture on the right is simply presented with the convention that the bottom of the stack is at the bottom of the picture.

The global execution context is at the bottom and won't be resumed until function A' execution contexts has been removed from the stack above it, and function A's execution context won't resume until function B's context has been removed from above it.

Execution contexts don't have "priority" in themselves, they maintain the correct sequence of program execution when functions are called and return to the function or global code that called them.

Note that some recent additions to JavaScript for asynchronous operation like the 'await` operator can internally remove and save execution contexts to be resumed later, after some kind of asynchronous event or operation occurs.

Also note that executing in "global context" means that the global object and lexical environment is all that there is in the code's scope chain.. This is true when global code is executed in a script file, but is also generally true for call outs from the event loop.

traktor
  • 17,588
  • 4
  • 32
  • 53