Execution Context Flashcards
What is an execution context
An execution context is the process by which JS executes code. During the context runtime, the parser parses the source code and allocates memory for the variables and functions. The source code is generated and gets executed.
How many types of execution contexts are there and what are they called?
There are two types of execution contexts: global and function.
- The global execution context (obExCo) is created when a JavaScript script first starts to run, and it represents the global scope in JavaScript.
- A function execution context (funcExCo) is created whenever a function is called, representing the function’s local scope.
What are the phases of an execution context?
There are two phases of JavaScript execution context:
- Creation phase: In this phase, the JavaScript engine creates the execution context and sets up the script’s environment. It determines the values of variables and functions and sets up the scope chain for the execution context.
- Execution phase: In this phase, the JavaScript engine executes the code in the execution context. It processes any statements or expressions in the script and evaluates any function calls.
How does an execution context organize itself?
It is divided into two components. One is memory and the other is code. It is important to remember that these phases and components are applicable to both global and functional execution contexts.
What happens during the creation phase?
- Creates a global object that is window in the browser and global in NodeJs.
- Sets up a memory for storing variables and functions.
- Stores the variables with values as undefined
- Stores function references.
What happens to variables during the execution phase?
It starts going through the entire code line by line from top to bottom. As soon as it encounters (for example) n = 5, it assigns the value 5 to ‘n’ in memory. Until now, the value of ‘n’ was undefined by default.
What happens to functions during the execution phase?
- As the function has been allocated in memory, when it gets to the invocation line it will create a new function execution context.
- The functional execution context has access to all the code of the global execution context though vice versa is not applicable.
- Once completed, the function will return any values, and this function execution context will be destroyed.
What happens when a function is invoked?
The function execution context is created and has two stages:
1. creation stage
2. execution stage
What happens during the function creation stage?
- Creation phase is the phase in which the JS engine has called a function but its execution has not started. In the creation phase, JS engine is in the compilation phase and it just scans over the function code to compile the code, it doesn’t execute any code.
- Creates the Activation/Variable object
- Creates the scope chain object
- Determines the value of the ‘this’ keyword
What is an activation/variable object and when is it created?
The activation/variable object is created during the creation stage of function execution context.
The activation/variable object is a special object in JS which contain all the variables, function arguments and inner functions declaration information.
What is the syntax of the function execution context (funcExCo) object?
executionContextObj = { Arguments: {}, variableObject: {}, scopechain: [], this }
What is the scope chain?
Scope in JavaScript is a mechanism that determines how accessible a piece of code is to other parts of the codebase. Scope answers the questions: from where can a piece of code be accessed? From where can’t it be accessed? What can access it, and what can’t?
Each Function Execution Context creates its scope: the space/environment where the variables and functions it defined can be accessed via a process called Scoping.
The JavaScript engine traversing up the scopes of the execution contexts is called the scope chain.
In a funcExCo where are the function arguments stored?
ArgumentObject in the variableObject
In funcExCo What is the syntax for an variable object during creation phase?
~~~
function funA (a, b) {
var c = 3;
var d = 2;
d = function() {
return a - b;
}
}
funA(3, 2);
~~~
variableObject = { argumentObject : { 0: a, 1: b, length: 2 }, a: 3, // taken from arguments b: 2 // taken from arguments c: undefined, d: undefined then pointer to the function defintion of d ?????? }
How are functions handled during creation phase of funcExCo?
When JS engine encounters a function definition inside the current function, it will create a new property by the name of the function. Function definitions in the creation phase are stored in heap memory, they are not stored in the execution context stack. Function name property points to its definition in the heap memory.