-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-1-4.js
230 lines (148 loc) · 5.58 KB
/
2-1-4.js
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
// Scopes
// In simple words, scope is simply where to look for things.
// We are looking for variables & functions
// We have three types of scopes in JS
// Global Scope
// Function Scope
// Block Scope
// Global Scope - If a variable is present in a global scope, then it is accessable everywhere in the JS File
// One of the way is to declare variable outside any function or a block
var name = "Mohit" ;
function greet()
{
console.log("greetings !", name);
function test() {
console.log("test");
}
}
function fun()
{
console.log("Have Fun !", name);
}
greet();
fun();
// test(); - gives an error as test is not global, it is inside a function.
// function scope - In a function scope, the visibility of a variable/function is just inside that function or is just inside the outer function
// example - function fun{
// var x=12; ->It is just accessable inside the function as its local to that function
// }
var name = "Mohit" ;
function greet()
{
var x=12;
console.log("greetings !", name);
function test() {
console.log("test",x);
}
test();
console.log(x);
}
function fun()
{
console.log("Have Fun !", name);
}
greet();
fun();
// Block Scope - In JS, we can use a pair of curly braces to declare a block
// example - {
// .......
// }
// Now, if a variable/function is only accessible/visible inside a block then it willbe having block scope.
// Exampele - if else, while, for or any raw block
if(true){
let x=10;
console.log(x);
}
// console.log(x);
// or
{
let x=10;
console.log(x);
}
// console.log(x);
// Difference b/w let, var and const
// var - whenever we use a var anywhere inside a function, the variable gets function scope. If we used it outside the function, no matter if it is enclosed in a block or not, it will give the variable global scope
// Example -
{
var m=21;
console.log(m);
}
console.log(m); // Here, m is printed as m is a block and thus the var m becomes global but if we put var inside a function, we wont be able to use it anywhere outside the function
// When we use a var outside from a block, then it gives us undefined but it doesnt gives us any error because of _______Lexical Scoping_______.
function snh()
{
var n=12;
console.log(n);
}
// console.log(n); //It throws error as var is only accessible inside the function, its local to the function
// let - whenever we initialize a variable with let, it always gets the scope of enclosing BLOCK.
{
let m=10;
console.log(m);
}
function funn() {
let n=5;
console.log(5);
}
funn();
console.log(m);
// let can't be printed inside a function scope before initialization as its not function scope.
// We cant redeclare a variable twice with let.
// if we declare let outside any block then it doesnt get ____complete____ global scope
// console.log(a); //It throws error but it wouldnt have been the same with var.
let a=5;
// Const - it is similar to let but it can neither be reassigned nor redeclared.
// e.g. let y=10; and the if we do y=11, it wont happen
// How JS Parses the code??
// We know, that JS is not interpreted, it is definitely hybrid. i.e compile + interpreted.
// So, whwnever we try to execute a JS code, JS first parses the whole code, in this phase it assigns scopes to variables/function.
// Once it is done, it reads the code and executes it.
// Every variable in our code will be used in one of the following ways:
// either it will be getting a value assigned i.e used as a target(x=10)
// or it will be used to retreive a value i.e as a source(y=10+x)
// Now, JS will start the parsing phase.
// Outside everything it maintains global scope, but the moment it goes inside a function it starts maintaining scope of that function aloso.
var teacher ="mohit";
function fun(){
var teacher = "abhay";
teachingAssistant="Hari"; //No formal declaration. So, once inside a scope, we dont know about scope of a variable, we check the outer scopes one by one.
console.log(teacher);
console.log(teachingAssistant);
}
fun();
console.log(teacher);
// console.log(teachingAssistant);
// Phase 1 -> parsing = we will do scope resolution
// whenever we declare a variable using var/let/const or initialization of a function it is a formal declaration
// So, in the parsing phase JS looks for formal declarations
// In this parsing phase, we only allocate scopes, we dont allocate values
// The moment we go in a functon, we maintain a new scope as well i.e function scope
// line 172 -
// _____Auto-Globals_____ - In JS if we keep on searching scope of a variable in outer scopes and nowhere found it, we automatically consider it in global scope
// Auto Globals happen during execution phase
var teacher ="mohit";
function fun(){
var teacher = "abhay";
teachingAssistant="Hari";
console.log(teacher);
console.log(teachingAssistant);
}
// console.log(teachingAssistant); // will give reference error as auto global not yet executed.
fun();
console.log(teacher);
// NOTE - Autoglobals onky work with target reference and not with source
var teacher1 ="mohit";
function fun(){
console.log(subject);
var teacher1 = "abhay";
teachingAssistant1="Hari";
var subject="Java Script";
console.log(teacher1);
console.log(teachingAssistant1);
}
fun();
console.log(teacher1);
console.log(teachingAssistant1);
// Difference b/w lexical and dynamic scoping
// Lexical - Scopes are assigned earlier and then while execution, they are only executed.
// Dynamic - Scopes are seen during the time of execution only.