-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26.hiding_scope_function.js
More file actions
93 lines (49 loc) · 1.13 KB
/
26.hiding_scope_function.js
File metadata and controls
93 lines (49 loc) · 1.13 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
// // 116 - problem
// function doSomething(a) {
// b = a + doSomethingElse(a * 2);
//
// console.log(b*3);
// }
//
// function doSomethingElse(a) {
// return a - 1;
// }
//
// var b;
//
// doSomething(2);
// console.log(b); // b also changed outside which is not desirable
// // 117 - so it is best to keep access to variables we don't need
// // in other places hidden
// // As it also saves the function from outside influences
// // it is a good software development practice
// function doSomething(a) {
// function doSomethingElse(a) {
// return a - 1;
// }
//
// var b = a + doSomethingElse(a * 2);
//
// console.log(b * 3);
// }
//
//
// doSomething(2);
// // 118 - If variables are not hidden, collisions can occur
// function test1() {
// b = 6;
// }
//
// var b = 2;
//
//
// console.log(b);
// test1();
// console.log(b); // b should be 2, but were modified from inside the function
// 119 - Hiding variables also gives you an ability to reuse the same variable name
// and you can avoid collisions
function test1() {
var b = 2;
console.log(b);
}
test1();