Foundation: Execution Context & Scopes Flashcards

1
Q

What does browser do when it sees script tag?

A

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.

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

What does execution context contain?

A

The Execution Context contains the code that’s currently running and everything that aids in its execution.

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

What does execution context do?

A

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.

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

What are the kinds of Execution Context in JavaScript:

A

Global Execution Context (GEC)
Function Execution Context (FEC)

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

What is Global Execution Context (GEC)

A

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.

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

What Function Execution Context (FEC)?

A

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.

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

Can their be multiple execution context?

A

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

Can their be multiple execution context?

A

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

How are Execution Contexts Created?

A

The creation of an Execution Context (GEC or FEC) happens in two phases:

Creation Phase
Execution Phase

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

What is Creation Phase in Execution Context?

A

The creation phase occurs in 3 stages, during which the properties of the Execution Context Object are defined and set. These stages are:

  1. Creation of the Variable Object (VO)
  2. Creation of the Scope Chain
  3. Setting the value of this keyword
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is Execution Context Object?

A

The Execution Context Object stores a lot of important data which the code in the Execution Context uses during its run-time.

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

What exact happens? Creation Phase: Variable Object (VO)

A
  1. 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.

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

What is Hoisting?

A

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).

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

What happens in Creation Phase: Creation of The Scope Chain?

A

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

What is lexical scoping?

A

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.

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

What is Creation Phase: “this” Keyword?

A

The JavaScript this keyword refers to the scope where an Execution Context belongs.
Once the scope chain is created, the value of ‘this’ is initialized by the JS engine.

Global Context
this refers to the global object — which is the window object.

Function Context
the value of this keyword the function has access to is that of the object it is defined in, not the global object.

Example:

const obj = {
name: “Billy”,
sing: function () {
return “lalala “ + this.name
},
}
obj.sing() // “lalala Billy”

17
Q

What are different Hoisting?

A

Function Hoisting: Doesn’t matter where you declare the function it’s hoisted
Variable Hoisting: var is initialized to undefined.

Ground Rules of Hoisting:
Hoisting only works for function declarations, not expressions.
variable hoisting does not work for variables initialized with the let or const keyword.

18
Q

Where is scope stored?

A

It’s [[scope]]

19
Q

Why do we use “use strict”?

A

Example,

function weird() {
height = 50;
}
weird()

Height will be stored in global scope with a value of 50.

20
Q

Why not just use global?

A

Memory
Variable collusion

21
Q

What is IIFE?

A

Immediately invoked function Expression. Used in something like jQuery and other libraries to simplify and not use global variables.

(function () {
})();

22
Q

What is Dynamic Scope vs Lexical Scope

A

Lexical scoping refers to when the location of a function’s definition determines which variables you have access to.
Dynamic scoping uses the location of the function’s invocation to determine which variables are available.

Example: “this” value in the closure can be solved with arrow functions.

23
Q

What is call, apply, bind?

A

The call is simply called the function. Example a() is a.call(); we can borrow methods from other objects.
The call can take parameters.

Apply:
Apply can take an array of paraments.

Bind:
Is similar to apply but doesn’t apply, it binds to a variable which can be used later.

24
Q

What is function currying?

A

Using bind we can curry a function, Basically reusing a function to a new function.

Example:
function sum(a, b) {
return a + b;
}
let curriedSum = _.curry(sum); // using _.curry from lodash library
alert( curriedSum(1, 2) ); // 3, still callable normally
alert( curriedSum(1)(2) ); // 3, called partially

25
Q

Difference between context and scope?

A

Scope pertains to the visibility of the variables
Context refers to the object within which a function is executed.