Execution Context Flashcards

1
Q

What is an execution context

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How many types of execution contexts are there and what are they called?

A

There are two types of execution contexts: global and function.

  1. The global execution context (obExCo) is created when a JavaScript script first starts to run, and it represents the global scope in JavaScript.
  2. A function execution context (funcExCo) is created whenever a function is called, representing the function’s local scope.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the phases of an execution context?

A

There are two phases of JavaScript execution context:

  1. 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.
  2. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does an execution context organize itself?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens during the creation phase?

A
  1. Creates a global object that is window in the browser and global in NodeJs.
  2. Sets up a memory for storing variables and functions.
  3. Stores the variables with values as undefined
  4. Stores function references.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens to variables during the execution phase?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens to functions during the execution phase?

A
  1. As the function has been allocated in memory, when it gets to the invocation line it will create a new function execution context.
  2. The functional execution context has access to all the code of the global execution context though vice versa is not applicable.
  3. Once completed, the function will return any values, and this function execution context will be destroyed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens when a function is invoked?

A

The function execution context is created and has two stages:
1. creation stage
2. execution stage

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens during the function creation stage?

A
  1. 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.
  2. Creates the Activation/Variable object
  3. Creates the scope chain object
  4. Determines the value of the ‘this’ keyword
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an activation/variable object and when is it created?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the syntax of the function execution context (funcExCo) object?

A
executionContextObj = {
Arguments: {},
   variableObject: {},
   scopechain: [],
   this
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the scope chain?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In a funcExCo where are the function arguments stored?

A

ArgumentObject in the variableObject

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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);
~~~

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How are functions handled during creation phase of funcExCo?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What happens during the execution phase of funcExCo?

A
17
Q

During the global creation stage how are functions handled?

A

For every function declaration, a property is added to the variable object, pointing to that function, and that property is stored in memory. This means that all the function declarations will be stored and made accessible inside the VO, even before the code starts running.

18
Q

What is hoisting?

A

Function and variable declarations are hoisted in JavaScript. This means that they are stored in memory of the current Execution Context’s VO and made available within the Execution Context even before the execution of the code begins. (Except const and var)

19
Q

Hoisting does not work for function expressions, why?

A

Hoisting only works for function declarations, not expressions. Here is an example of a function expression where the code execution will break. Hoisting is part of the creation phase whereas function expressions are part of the execution phase.

20
Q

How does hoisting work for let and const? And what will happen if you try to access these variables, before they’ve been assigned a value?

A

Variable hoisting does not work for variables initialized with the let or const keyword. Trying to access a variable ahead of declaration and use the let and const keywords to declare it later will result in a ReferenceError.

21
Q

What is the name for the period during execution where let/const variables are hoisted but not accessible?

A

Temporal Dead Zone

22
Q

When a function is defined in another function, the inner function has access to the code defined in that of the outer function, and that of its parents. What is this behavior is called?

A

Lexical scoping

23
Q

What is the arguments object?

A

A key:value pair of parameters anticipated inside a function is stored in the arguments object. It also has a default property called length, which counts how many parameters that function has. When the function’s argument is empty, the argument object defaults to a length: 0.

24
Q

During the creation phase, two-state components are created:
Lexical Environment
Variable Environment
What are they used for? ??????

A

A Lexical Environment component is a structure based on the lexical nesting structure of JavaScript code that defines the association of identifiers to the values of variables and functions. Binding is the process of associating an identifier with the values of variables and functions.

A Variable Environment component is a Lexical Environment that defines the relationship between identifiers and the values of variables, but not functions.

The difference between these two is that the identifier is bounded in the variable. The Lexical Environment stores identifier bindings to the values of variables (let and const) and functions, whereas the Variable Environment simply stores identifier bindings to the values of the variable (var).