-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.js
More file actions
455 lines (300 loc) · 6.08 KB
/
Copy pathscope.js
File metadata and controls
455 lines (300 loc) · 6.08 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//scope - functional scope, global scope and block scope
//function scope - function k andar hi use ho skti hai
//global scope- poore code me kahi bhi use ho skti hai
//block scope- {curly braces me hi use ho sakti hai}
//agar aapka code kisi bhi {} k andar nhi hai to aapka code global hai !
//Lexical Scoping and Dynamic Scoping
//lexical -- js me hum lexical scoping hi use karte hai-- mltb jahan hum physically available hai wo location decide karega hum kya access kar sakte hai aur kya nhi!
//example:- a is lexically(physically) available inside the function abcd that means it can be accessed anywhere inside the function abcd.so a is lexically scoped in abcd.
function abcd(){
let a = 12;
function defg(){
console.log(a);
}
}
//Dynamic Scoping:- kahan se call kar rhe ho uspe depend karega ki kya value milegi.(yeh js me follow nhi karte hai)
//example:- agar dynamic scoping hoti js me to x ki value 13 print hoti kyuki wo x ki value wahi lmno function se utha leta, but hijk globally scoped hai to jab hijk call hua to x ka value global scope me dhunda jaega aur wahan x ki value 12 hai to 12 print hogi.
let x =12;
function hijk(){
console.log(x);
}
function lmno(){
let x = 13;
hijk();
}
lmno();
/* Bilkul bhai. Chalo ekdum **scratch se**, bina definitions ratwaye, real-life logic ke saath samajhte hain.
---
# Step 1: Scope kya hota hai?
Scope simply batata hai:
> "Ye variable kis jagah se access ho sakta hai?"
```js
let college = "HIT";
function show() {
console.log(college);
}
show();
```
Yahan `college` function ke andar access ho raha hai kyunki wo outer scope mein hai.
---
# Step 2: Lexical ka matlab kya hai?
**Lexical = Code mein likha hua structure**
JavaScript code ko dekhkar pehle hi decide kar leta hai:
```text
Kaun kiske andar likha hua hai?
```
Example:
```js
let x = 10;
function outer() {
let y = 20;
function inner() {
console.log(x, y);
}
inner();
}
```
Code structure:
```text
Global
│
└── outer()
│
└── inner()
```
`inner()` ke paas access hai:
* Apne variables
* outer() ke variables
* Global ke variables
Kyuki wo code mein unke andar likha hua hai.
---
# Step 3: Lexical Scope ka Rule
Variable dhundte waqt JS ye nahi dekhta:
```text
Function ko kisne call kiya?
```
Wo dekhta hai:
```text
Function kahaan define hua tha?
```
Ye sabse important line hai.
---
# Example 1
```js
let name = "Shivam";
function printName() {
console.log(name);
}
printName();
```
Output:
```text
Shivam
```
Simple hai.
---
# Example 2
Ab thoda interesting.
```js
let name = "Shivam";
function printName() {
console.log(name);
}
function test() {
let name = "Rahul";
printName();
}
test();
```
Ab guess karo output?
---
### Output
```text
Shivam
```
Not Rahul.
---
## Kyun?
Code structure dekho:
```text
Global
│
├── name = "Shivam"
│
├── printName()
│
└── test()
```
`printName()` Global ke andar define hua tha.
Jab JS `name` dhundta hai:
```text
printName()
↑
Global
```
Usko `"Shivam"` mil jata hai.
Wo `test()` ke andar nahi dekhta.
---
# Tumhari Galti Yahan Ho Sakti Hai
Tum soch rahe hoge:
```text
Arre printName() ko to test() ne call kiya tha.
To Rahul kyu nahi aaya?
```
Because JS lexical scope use karta hai.
Caller matter nahi karta.
Definition place matter karti hai.
---
# Dynamic Scope Hota To?
Maan lo JS dynamic hota.
Fir lookup aise hota:
```text
printName()
↑
test()
↑
Global
```
Tab output aata:
```text
Rahul
```
Lekin JS aisa nahi karta.
---
# Real Life Analogy
Maan lo tum hostel mein rehte ho.
Tumhara permanent address:
```text
Room 101
```
Ab tum kisi dost ke room mein chale gaye:
```text
Room 205
```
Koi puche:
```text
Tumhara room number kya hai?
```
Tum bologe:
```text
101
```
Kyuki tumhara original ghar 101 hai.
205 nahi.
Exactly waise hi function apna original scope yaad rakhta hai.
---
# Execution Context Kahan Aata Hai?
Jab function execute hota hai:
```js
function greet() {
let msg = "Hello";
console.log(msg);
}
greet();
```
JS ek execution context banata hai.
```text
Execution Context
------------------
Variables
Functions
this
```
---
# Visual
```text
Call Stack
┌─────────┐
│ greet() │
├─────────┤
│ Global │
└─────────┘
```
Jab function finish:
```text
┌─────────┐
│ Global │
└─────────┘
```
Context destroy.
---
# Closure Kahan Aata Hai?
Ab ye dekho:
```js
function outer() {
let count = 0;
return function inner() {
count++;
console.log(count);
};
}
const counter = outer();
counter();
counter();
counter();
```
Output:
```text
1
2
3
```
---
## Normal Rule Ke Hisaab Se
Jab `outer()` finish hua:
```text
outer context destroy
```
hona chahiye tha.
To `count` bhi gayab ho jana chahiye tha.
---
## Lekin Hua Kya?
`inner()` ko abhi bhi `count` chahiye.
To JS bolta hai:
```text
Theek hai,
main count ko memory mein rakhta hu.
```
Ye preserved memory hi closure hai.
---
# Visual Diagram
```text
outer()
─────────────────
count = 0
▲
│
│ remembered
│
inner()
```
Jab bhi:
```js
counter();
```
run hoga,
`inner()` ko `count` mil jayega.
---
# Ab Puri Story Connect Karo
### 1. Lexical Scope
```text
Function apna parent scope
yaad rakhta hai.
```
---
### 2. Execution Context
```text
Function run hone par
temporary workspace banta hai.
```
---
### 3. Closure
```text
Function khatam hone ke baad bhi
agar inner function ko variable chahiye,
to JS us variable ko preserve karta hai.
```
---
# Interview Answer (30 sec)
> JavaScript lexical scoping follow karta hai. Iska matlab variable lookup function ke definition location par depend karta hai, na ki call location par. Jab function execute hota hai to execution context create hota hai. Agar koi inner function outer scope ke variables ko use karta rahe after outer function has finished, JavaScript closure create karke un variables ko memory mein preserve rakhta hai.
*/