Foundation: Execution Context & Scopes Flashcards
What does browser do when it sees script tag?
While reading through HTML, if the browser encounters JavaScript code to run via a tag or an attribute that contains JavaScript code like onClick, it sends it to its JavaScript engine.
The browser’s JavaScript engine then creates a particular environment to handle the transformation and execution of this JavaScript code. This environment is known as the Execution Context.
What does execution context contain?
The Execution Context contains the code that’s currently running and everything that aids in its execution.
What does execution context do?
During the Execution Context run-time, the specific code gets parsed by a parser, the variables and functions are stored in memory, executable byte-code gets generated, and the code gets executed.
What are the kinds of Execution Context in JavaScript:
Global Execution Context (GEC)
Function Execution Context (FEC)
What is Global Execution Context (GEC)
Whenever the JavaScript engine receives a script file, it first creates a default Execution Context known as the Global Execution Context (GEC).
The GEC is the base/default Execution Context where all JavaScript code not inside a function gets executed.
For every JavaScript file, there can only be one GEC.
What Function Execution Context (FEC)?
Whenever a function is called, the JavaScript engine creates a different type of Execution Context known as a Function Execution Context (FEC) within the GEC to evaluate and execute the code within that function.
Since every function call gets its own FEC, there can be more than one FEC in the run-time of a script.
Can their be multiple execution context?
For every JavaScript file, there can only be one GEC.
Since every function call gets its own FEC, there can be more than one FEC in the run-time of a script.
Can their be multiple execution context?
For every JavaScript file, there can only be one GEC.
Since every function call gets its own FEC, there can be more than one FEC in the run-time of a script.
How are Execution Contexts Created?
The creation of an Execution Context (GEC or FEC) happens in two phases:
Creation Phase
Execution Phase
What is Creation Phase in Execution Context?
The creation phase occurs in 3 stages, during which the properties of the Execution Context Object are defined and set. These stages are:
- Creation of the Variable Object (VO)
- Creation of the Scope Chain
- Setting the value of this keyword
What is Execution Context Object?
The Execution Context Object stores a lot of important data which the code in the Execution Context uses during its run-time.
What exact happens? Creation Phase: Variable Object (VO)
- It stores the variables and function declarations defined within that Execution Context.
In GEC:
1. In the GEC, for each variable declared with the var keyword, a property is added to VO that points to that variable and is set to ‘undefined’.
2. For every function declaration, a property is added to the VO, 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.
In FEC:
1. It does not construct a VO. Instead, it generates an array-like object called the ‘argument’ object, which includes all of the arguments supplied to the function.
What is Hoisting?
The process of storing variables and function declarations in memory before the execution of the code is known as Hoisting.
It happens in Creation Phase: Creation Of The Variable Object (VO).
What happens in Creation Phase: Creation of The Scope Chain?
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.
This means the position of something within a codebase, that is, where a piece of code is located.
So global variables can be used anywhere.
var global_variable = 20;
function main_function() {
// Local Variable
var local_variable = 30;
var nested_function = function () { // Display the value inside the local variable console.log(local_variable); } var another_nested_function = function () { // Displays the value inside the global variable console.log(global_variable); } nested_function(); another_nested_function(); } main_function();
What is lexical scoping?
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. This behaviour is called lexical scoping.
Lexical scoping is also known as static scoping.
Lexical scoping in JavaScript allows for the concept of closures.
Most languages use lexical scoping because it tends to promote source code that is more easily understood.