liveBook · Manning

Function execution context

As explained in Secrets of the JavaScript Ninja, Second Edition

An execution context created for evaluating a particular function invocation in JavaScript.

When one function calls another, JavaScript must keep track of the active work and where to resume. A function execution context is an internal runtime context created for each function invocation; it is not the function context accessed through `this`. section 5.3

During a function call:

  • The current context pauses as JavaScript creates a new function execution context to evaluate the called function. section 5.3
  • After the function finishes, its context is usually discarded and the caller’s context is restored. The execution context stack tracks active and waiting contexts. section 5.3
Listing 5.5. The creation of execution contexts
  function skulk(ninja) {           #A
    report(ninja + " skulking");    #A
  }                                 #A

  function report(message) {        #B
    console.log(message);           #B
  }                                 #B

  skulk("Kuma");                    #C
  skulk("Yoshi");                   #C

Trace `skulk` calling `report`, then compare repeated calls to see where execution must resume. section 5.3

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Explains per-invocation creation, suspension, discard, and caller restoration, then introduces stack tracking without detailing context internals.
Examples
Uses nested `skulk` and `report` calls, repeated invocations, and figures of stack behavior and a Chrome DevTools stack.
What it assumes
Assumes familiarity with global and function code, and distinguishes execution context from function context accessed through `this`.
What you can do after
Understand how nested function calls pause and resume through the execution context stack.
How it connects
Connects function execution contexts to global execution context, single-threaded execution, and the execution context stack.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.