-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathScopes and Namespaces.js
More file actions
111 lines (63 loc) · 2.96 KB
/
Scopes and Namespaces.js
File metadata and controls
111 lines (63 loc) · 2.96 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
// Although JavaScript deals fine with the syntax of two matching curly braces for blocks,
// it does not support block scope; hence, all that is left in the language is function scope.
function test() { // a scope
for(var i = 0; i < 10; i++) { // not a scope
// count
}
console.log(i); // 10
}
// There are also no distinct namespaces in JavaScript, which means that everything gets defined in one globally shared namespace.
// Each time a variable is referenced, JavaScript will traverse upwards through all the scopes until it finds it.
// In the case that it reaches the global scope and still has not found the requested name, it will raise a ReferenceError.
// The Bane of Global Variables
// script A
// foo = '42';
// script B
var foo = '42'
// The above two scripts do not have the same effect.
// Script A defines a variable called foo in the global scope, and script B defines a foo in the current scope.
function test_1() {
// local scope
foo = 21; // Leaving out the var statement inside the function test will override the value of foo.
}
// While this might not seem like a big deal at first, having thousands of lines of JavaScript and not using var will introduce horrible,
// hard-to-track-down bugs.
console.log(foo);
// global scope
var items = [/* some list */];
for(var i = 0; i < 10; i++) {
subLoop();
}
function subLoop() {
// scope of subLoop
for(i = 0; i < 10; i++) {
// missing var statement
// do amazing stuff!
}
}
// The outer loop will terminate after the first call to subLoop, since subLoop overwrites the global value of i.
// Using a var for the second for loop would have easily avoided this error.
// The var statement should never be left out unless the desired effect is to affect the outer scope.
// Local Variables
// The only source for local variables in JavaScript are function parameters and variables declared via the var statement.
// global scope
var foo = 1;
var bar = 2;
var j = 2;
(function testing(j) {
// local scope of the function test
j = 5;
console.log(j);
var foo = 3;
bar = 4;
})()
// console.log(j);
// While foo and i are local variables inside the scope of the function test, the assignment of bar will override the global variable with the same name.
// Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current
// function).
// JavaScript hoists declarations. This means that both var statements and function declarations will be moved to the top of their enclosing scope.
// To know more about Hosting refer Javascript Hoisting.js
// Namespaces
// A common problem associated with having only one global namespace is the likelihood of running into problems where variable names clash.
// In JavaScript, this problem can easily be avoided with the help of anonymous wrappers.
// For Namespaces in javascript kindley refer JavaScript Namespaces.js