forked from loaialaddien/javascriptNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variableScope.js
61 lines (50 loc) · 1.42 KB
/
variableScope.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
(function() {
try {
throw new Error();
} catch (x) {
var x = 1,
y = 2;
console.log(x);
}
console.log(x);
console.log(y);
})();
(function() {
var x, y; // outer and hoisted
try {
throw new Error();
} catch (x /* inner */ ) {
x = 1; // inner x, not the outer one
y = 2; // there is only one y, which is in the outer scope
console.log(x /* inner */ );
}
console.log(x);
console.log(y);
})();
//variable scope
//No Block-Level Scope
//Local Variables Have Priority Over Global Variables in Functions
var name = "Richard";
// the blocks in this if statement do not create a local context for the name variable
if (name) {
name = "Jack"; // this name is the global name variable and it is being changed to "Jack" here
console.log(name); // Jack: still the global variable
}
// Here, the name variable is the same global name variable, but it was changed in the if statement
console.log(name); // Jack
//////////////////
for (var i = 0; i < 5; i++) {
setTimeout(function() { console.log(i); }, i * 1000);
}
//5 5 5 5 5
//solutions
for (let i = 0; i < 5; i++) {
setTimeout(function() { console.log(i); }, i * 1000);
}
//0 1 2 3 4
for (var i = 0; i < 5; i++) {
(function(x) {
setTimeout(function() { console.log(x); }, x * 1000);
})(i);
}
//0 1 2 3 4