-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutionContext.js
More file actions
363 lines (258 loc) · 5.98 KB
/
Copy pathexecutionContext.js
File metadata and controls
363 lines (258 loc) · 5.98 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// execution context ek hypothetical dabba hai - JS function dekhte hi ek dabba banati hai jo dikhta nhi actually me aur function do phases me execute hota hai ek memory phase aur dusra execution phase.
//JS sabse pehle jaise hi aapka function dekhta hai to sabse pehle execution context banata hai, ye ek process hai jo ki do different phases me chalta hai, memory phase and execution phase
// for better understanding - have a look on the notes "Scope,ExecutionContext,Closures" and "diagramforExecutionContext"
/*
Let's use a **real code example** and trace exactly what JavaScript does behind the scenes.
---
# Code Example
```js
var username = "Shivam";
function greet() {
var message = "Welcome";
function showMessage() {
console.log(username + " : " + message);
}
showMessage();
}
greet();
```
Output:
```js
Shivam : Welcome
```
---
# Step 1: Global Execution Context Creation
When JS starts, it creates the **Global Execution Context (GEC)**.
## Memory Creation Phase
```text
Global Memory
────────────────────
username → undefined
greet → function
```
---
## Execution Phase
```js
var username = "Shivam";
```
Now memory becomes:
```text
Global Memory
────────────────────
username → "Shivam"
greet → function
```
---
# Step 2: greet() is Called
```js
greet();
```
A new **Function Execution Context** is pushed onto the Call Stack.
---
## Call Stack
```text
┌─────────────┐
│ greet() │
├─────────────┤
│ Global │
└─────────────┘
```
---
## greet() Memory Creation Phase
```text
greet Memory
────────────────────
message → undefined
showMessage → function
```
---
## greet() Execution Phase
```js
var message = "Welcome";
```
Memory becomes:
```text
greet Memory
────────────────────
message → "Welcome"
showMessage → function
```
---
# Step 3: showMessage() is Called
```js
showMessage();
```
Another execution context is created.
---
## Call Stack
```text
┌─────────────────┐
│ showMessage() │
├─────────────────┤
│ greet() │
├─────────────────┤
│ Global │
└─────────────────┘
```
---
# Variable Lookup Process
Inside:
```js
console.log(username + " : " + message);
```
JS searches variables like this:
### Looking for `username`
```text
showMessage()
↓ Not Found
greet()
↓ Not Found
Global
↓ Found!
```
Value:
```js
"Shivam"
```
---
### Looking for `message`
```text
showMessage()
↓ Not Found
greet()
↓ Found!
```
Value:
```js
"Welcome"
```
---
# Visual Diagram
```text
GLOBAL EXECUTION CONTEXT
─────────────────────────────
username → "Shivam"
greet() → function
│
▼
greet() EXECUTION CONTEXT
─────────────────────────────
message → "Welcome"
showMessage() → function
│
▼
showMessage() EXECUTION CONTEXT
─────────────────────────────
console.log(username + message)
Search Path:
message → greet()
username → Global
```
---
# Complete Call Stack Flow
### Initial
```text
┌─────────┐
│ Global │
└─────────┘
```
### greet() Called
```text
┌─────────┐
│ greet() │
├─────────┤
│ Global │
└─────────┘
```
### showMessage() Called
```text
┌─────────────────┐
│ showMessage() │
├─────────────────┤
│ greet() │
├─────────────────┤
│ Global │
└─────────────────┘
```
### showMessage() Finishes
```text
┌─────────┐
│ greet() │
├─────────┤
│ Global │
└─────────┘
```
### greet() Finishes
```text
┌─────────┐
│ Global │
└─────────┘
```
---
# How Closure Fits In
Now slightly modify the code:
```js
function greet() {
let message = "Welcome";
return function() {
console.log(message);
};
}
const fn = greet();
fn();
```
Normally, when `greet()` finishes:
```text
greet() removed from Call Stack
```
But the inner function still needs:
```js
message
```
So JavaScript preserves that memory.
---
## Closure Diagram
```text
greet()
──────────────────
message = "Welcome"
▲
│
│ remembered
│
Inner Function
│
▼
fn()
```
Even after `greet()` is gone from the Call Stack:
```text
Call Stack
──────────────
Global
```
the variable still exists because the returned function references it.
---
# The Interview-Worthy Mental Model
Think of an **Execution Context** as a folder.
```text
Global Folder
│
├── username
├── greet()
│
└── greet Folder
│
├── message
├── showMessage()
│
└── showMessage Folder
```
Whenever a function runs:
1. JS creates a new folder (Execution Context).
2. Stores local variables inside it.
3. Pushes it onto the Call Stack.
4. Removes it when the function finishes.
5. If an inner function still needs data, JS keeps that data alive using a **Closure**.
That's the complete relationship between **Scope → Execution Context → Call Stack → Closure**. Understanding this flow makes topics like hoisting, lexical environments, event loop, and `this` much easier.
*/