@@ -6,16 +6,15 @@ scope*.
6
6
7
7
> ** Note:** When not used in an assignment or as a function argument, the ` {...} `
8
8
> notation will get interpreted as a block statement and ** not** as an ` Object ` .
9
- > This, in conjunction with
10
- > [ automatic insertion of semicolons] ( #semicolon ) , can lead
11
- > to subtle errors.
9
+ > This, in conjunction with [ automatic insertion of semicolons] ( #semicolon ) ,
10
+ > can lead to subtle errors.
12
11
13
- Additionally, there are no distinct namespaces in JavaScript, this means that
12
+ Additionally, there are no distinct namespaces in JavaScript. This means that
14
13
everything gets defined in ** one** globally shared namespace.
15
14
16
- Each time one references a variable, JavaScript will traverse through the scopes
17
- upwards until it finds it. In the case that it reaches the global scope and still
18
- can't find the requested name it will raise a ` ReferenceError ` .
15
+ Each time a variable is referenced , JavaScript will traverse upwards through all
16
+ the scopes until it finds it. In the case that it reaches the global scope and
17
+ still has not found the requested name, it will raise a ` ReferenceError ` .
19
18
20
19
### The Bane of global Variables
21
20
@@ -29,8 +28,8 @@ The above two scripts do **not** have the same effect. Script A defines a
29
28
variable called ` foo ` in the * global* scope and script B defines a ` foo ` in the
30
29
* local* scope.
31
30
32
- Again, that's ** not** at all the same effect, forgetting to use a ` var ` can have
33
- major implications.
31
+ Again, that is ** not** at all the same effect, not using ` var ` can have major
32
+ implications.
34
33
35
34
// global scope
36
35
var foo = 42;
@@ -42,10 +41,9 @@ major implications.
42
41
foo; // 21
43
42
44
43
Leaving out the ` var ` statement will override the value of ` foo ` , this might not
45
- seem like a big deal at first, but consider you have a ten-thousand line
44
+ seem like a big deal at first, but consider having a ten-thousand line
46
45
JavaScript file with lots and lots of different variable names, not using ` var `
47
- will introduce bugs for sure. And additionally those bugs are very often hard to
48
- track down.
46
+ will introduce hard to track down bugs.
49
47
50
48
For example, when using generic variable names like ` i ` in loops.
51
49
@@ -63,43 +61,42 @@ For example, when using generic variable names like `i` in loops.
63
61
}
64
62
65
63
The outer loop will terminate after the first call to ` subLoop ` since that
66
- function had overriden the global value of ` i ` . Using a ` var ` for the second
67
- ` for ` loop would have easily avoided this, therefore never leave out ` var `
68
- unless you really want to access the variable of an outer scope.
64
+ function overwrites the global value of ` i ` . Using a ` var ` for the second
65
+ ` for ` loop would have easily avoided this error , therefore ` var ` should never be
66
+ left out unless the desired effect ** is ** to affect the outer scope.
69
67
70
- ### Local Variables
68
+ ### Local variables
71
69
72
- If one wants to declare a variable * local* to the current scope thes have to use
73
- the ` var ` keyword. ** Always** use the ` var ` keyword when declaring variables
74
- otherwise you might ** overriding** things that were already defined in outer
75
- scopes.
70
+ The only source for local variables in JavaScript are [ function] ( #functions )
71
+ parameters and variables that were declared with the ` var ` statement.
76
72
77
73
// global scope
78
74
var foo = 1;
79
75
var bar = 2;
76
+ var i = 2;
80
77
81
- function test() {
82
-
78
+ function test(i) {
83
79
// local scope of the function test
80
+ i = 5;
81
+
84
82
var foo = 3;
85
83
bar = 4;
86
84
}
85
+ test(10);
87
86
88
- In the above, ` var foo ` inside of ` test ` will create a new variable that is in
89
- the * local* scope. Therefore the value of the * global* ` foo ` does ** not** get
90
- changed. But the assignment ` bar = 4 ` will override the value of the * global*
91
- ` bar ` due to the missing ` var ` keyword.
87
+ While ` foo ` and ` i ` are local variables inside the scope of the function ` test ` ,
88
+ the assignment of ` bar ` will override the global variable.
92
89
93
- ### Name Resolution Order
90
+ ### Name resolution order
94
91
95
- All scopes in JavaScript - including the global one, have the name
96
- [ this] ( #this ) defined in them, which refers to the
97
- "current object". Function scopes also have the name
98
- [ arguments] ( #arguments ) defined, which contains the arguments that were
99
- passed to a function.
92
+ All scopes in JavaScript - including the global one - have the name
93
+ [ this] ( #this ) defined in them, which refers to the "current object".
100
94
101
- For example, when you try to access a variable named ` foo ` inside a function
102
- scope, JavaScript will lookup the name in the following order:
95
+ Function scopes also have the name [ arguments] ( #arguments ) defined, which
96
+ contains the arguments that were passed to a function.
97
+
98
+ For example, when trying to access a variable named ` foo ` inside the scope of a
99
+ function, JavaScript will lookup the name in the following order:
103
100
104
101
1 . In case there's a ` var foo ` statement in the current scope use that.
105
102
2 . If one of the function parameters is named ` foo ` use that.
@@ -111,9 +108,9 @@ scope, JavaScript will lookup the name in the following order:
111
108
112
109
### Namespaces
113
110
114
- One common problem of having only one global namespace is, that its very easy to
115
- run into problems where variable names clash. Luckily this can be easily avoided
116
- with the help of * anoynmous function wrappers* .
111
+ A common problem of having only one global namespace is that it is very easy to
112
+ run into problems where variable names clash. But this can be easily avoided
113
+ with the help of anonymous * function wrappers* .
117
114
118
115
(function() {
119
116
// a self contained "namespace"
@@ -124,16 +121,29 @@ with the help of *anoynmous function wrappers*.
124
121
125
122
})(); // execute the function immediately
126
123
127
- By default you cannot just call a function, you need to ** evaluate** it first.
128
- In this example, this is done by wrapping the
129
- [ function expression] ( #functions ) in parenthesis. While this is
130
- the most common style to do this, everything else that forces the function to be
131
- evaluated works just as well, like for example ` +function(){}() ` .
124
+
125
+ Since a unnamed functions are not considered as [ statement] ( #functions ) , they
126
+ get interpreted as expression, in order to run those functions they must fist be
127
+ evaluate and then called.
128
+
129
+ ( // evaluate the function inside the paranthesis
130
+ function() {}
131
+ ) // and return the function object
132
+ () // call the result of the evaluation
133
+
134
+ There are other ways for evaluating and calling the function expression which -
135
+ while different in syntax - do the exact same thing.
136
+
137
+ // Two other ways
138
+ +function(){}();
139
+ (function(){}());
132
140
133
141
### Best Practices
134
142
135
- Always use the * anonymous wrapper* to encapsulate your code in case there's any
136
- chance it might get used by someone else in their project. Also never define any
137
- variables in the * global* namespace, always use ` var ` to limit the scope of your
138
- variables.
143
+ It is recommended to always use an * anonymous wrapper* for encapsulating code in
144
+ its own namespace. This does not only protect against name clashes, but it also
145
+ allows for better modularization.
146
+
147
+ Additionally the use of global variables is consider ** bad practice** , any use
148
+ of them indicates badly written, hard to maintain code.
139
149
0 commit comments