-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript_runtime.js
More file actions
134 lines (79 loc) · 5.28 KB
/
javascript_runtime.js
File metadata and controls
134 lines (79 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ********************************execution context**********************************
(what is EC)=>
When the JavaScript engine scans a script file, it makes an environment called the Execution Context that handles the entire transformation and execution of the 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.
(types of EC)=>
There are two types of execution contexts: global and function. The global execution context is created when a JavaScript script first starts to run, and it represents the global scope in JavaScript. A function execution context is created whenever a function is called, representing the function's local scope.
Let’s start with the following example:
let x = 10;
function timesTen(a){
return a * 10;
}
let y = timesTen(x);
console.log(y); // 100
Each execution context has two phases: the creation phase and the execution phase.
****The creation phase***
When the JavaScript engine executes a script for the first time, it creates the global execution context. During this phase, the JavaScript engine performs the following tasks:
1)Create the global object i.e., window in the web browser or global in Node.js.
2)Create the this object and bind it to the global object.
3)Setup a memory heap for storing variables and function references.
4)Store the function declarations in the memory heap and variables within the global execution context with the initial values as undefined.
**for above code it will do following task=>
1)First, store the variables x and y and function declaration timesTen() in the global execution context.
2)Second, initialize the variables x and y to undefined.
2)The execution phase=>
During the execution phase, the JavaScript engine executes the code line by line, assigns the values to variables, and executes the function calls.
For each function call, the JavaScript engine creates a new function execution context.
The function execution context is similar to the global execution context. But instead of creating the global object, the JavaScript engine creates the arguments object that is a reference to all the parameters of the function:
During the execution phase of the function execution context, the JavaScript engine assigns 10 to the parameter a and returns the result (100) to the global execution context:
To keep track of all the execution contexts, including the global execution context and function execution contexts, the JavaScript engine uses the call stack,
******************Introduction to JavaScript Call Stack************
A call stack is a way for the JavaScript engine to keep track of its place in code that calls multiple functions. It has the information on what function is currently being run and what functions are invoked from within that function…
its work on LIFO principle
************************Stack overflow**********************
The call stack has a fixed size, depending on the implementation of the host environment, either the web browser or Node.js.
If the number of execution contexts exceeds the size of the stack, a stack overflow error will occur.
For example, when you execute a recursive function that has no exit condition, the JavaScript engine will issue a stack overflow error:
function fn() {
fn();
}
fn(); // stack overflow
*****************************Variable Scope***************************************
Scope determines the visibility and accessibility of a variable. JavaScript has three scopes:
1) The global scope
2) Local scope
3) Block scope (started from ES6)
1)The global scope
When the JavaScript engine executes a script, it creates a global execution context.
Also, it also assigns variables that you declare outside of functions to the global execution context. These variables are in the global scope. They are also known as global variables.
See the following example:
var message = 'Hi';
See the following example:
var message = 'Hi';
2) Local scope
The variables that you declare inside a function are local to the function. They are called local variables. For example:
var message = 'Hi';
function say() {
var message = 'Hello';
console.log(message);
}
say();
console.log(message);
When the JavaScript engine executes the say() function, it creates a function execution context. The variable message declared inside the say() function is bound to the function execution context of the function, not the global execution context.
JavaScript Local Variables
3)Block scope
ES6 provides the let and const keywords that allow you to declare variables in block scope.
Generally, whenever you see curly brackets {}, it is a block. It can be the area within the if, else, switch conditions or for, do while, and while loops.
See the following example:
function say(message) {
if(!message) {
let greeting = 'Hello'; // block scope
console.log(greeting);
}
// say it again ?
console.log(greeting); // ReferenceError
}
say();
Code language: JavaScript (javascript)
In this example, we reference the variable greeting outside the if block that results in an error.
*/