@@ -16,7 +16,7 @@ is in the language is *function scope*.
16
16
> ** not** as an object literal. This, in conjunction with
17
17
> [ automatic insertion of semicolons] ( #core.semicolon ) , can lead to subtle errors.
18
18
19
- There are also no distinct namespaces in JavaScript, that means that everything
19
+ There are also no distinct namespaces in JavaScript, which means that everything
20
20
gets defined in one * globally shared* namespace.
21
21
22
22
Each time a variable is referenced, JavaScript will traverse upwards through all
@@ -32,10 +32,10 @@ still has not found the requested name, it will raise a `ReferenceError`.
32
32
var foo = '42'
33
33
34
34
The above two scripts do ** not** have the same effect. Script A defines a
35
- variable called ` foo ` in the * global* scope and script B defines a ` foo ` in the
35
+ variable called ` foo ` in the * global* scope, and script B defines a ` foo ` in the
36
36
* current* scope.
37
37
38
- Again, that is ** not** at all the * same effect* , not using ` var ` can have major
38
+ Again, that is ** not** at all the * same effect* : not using ` var ` can have major
39
39
implications.
40
40
41
41
// global scope
@@ -49,8 +49,8 @@ implications.
49
49
50
50
Leaving out the ` var ` statement inside the function ` test ` will override the
51
51
value of ` foo ` . While this might not seem like a big deal at first, having
52
- thousands of lines of JavaScript and not using ` var ` will introduce horrible and
53
- hard to track down bugs.
52
+ thousands of lines of JavaScript and not using ` var ` will introduce horrible,
53
+ hard-to- track- down bugs.
54
54
55
55
// global scope
56
56
var items = [ /* some list * /] ;
@@ -116,7 +116,7 @@ JavaScript **hoists** declarations. This means that both `var` statements and
116
116
}
117
117
118
118
The above code gets transformed before any execution is started. JavaScript moves
119
- the ` var ` statements as well as the ` function ` declarations to the top of the
119
+ the ` var ` statements, as well as the ` function ` declarations to the top of the
120
120
nearest surrounding scope.
121
121
122
122
// var statements got moved here
@@ -146,11 +146,11 @@ Missing block scoping will not only move `var` statements out of loops and
146
146
their bodies, it will also make the results of certain ` if ` constructs
147
147
non-intuitive.
148
148
149
- In the original code the ` if ` statement seemed to modify the * global
150
- variable* ` goo ` , while actually it modifies the * local variable* - after hoisting
149
+ In the original code, although the ` if ` statement seemed to modify the * global
150
+ variable* ` goo ` , it actually modifies the * local variable* - after hoisting
151
151
has been applied.
152
152
153
- Without the knowledge about * hoisting* , below code might seem to raise a
153
+ Without the knowledge about * hoisting* , the below code might seem to raise a
154
154
` ReferenceError ` .
155
155
156
156
// check whether SomeImportantThing has been initiliazed
@@ -173,18 +173,18 @@ moved to the top of the *global scope*.
173
173
### Name Resolution Order
174
174
175
175
All scopes in JavaScript, including the * global scope* , have the special name
176
- [ ` this ` ] ( #function.this ) defined in them, which refers to the * current object* .
176
+ [ ` this ` ] ( #function.this ) , defined in them, which refers to the * current object* .
177
177
178
- Function scopes also have the name [ ` arguments ` ] ( #function.arguments ) defined in
179
- them which contains the arguments that were passed to a function.
178
+ Function scopes also have the name [ ` arguments ` ] ( #function.arguments ) , defined in
179
+ them, which contains the arguments that were passed to a function.
180
180
181
181
For example, when trying to access a variable named ` foo ` inside the scope of a
182
182
function, JavaScript will lookup the name in the following order:
183
183
184
- 1 . In case there is a ` var foo ` statement in the current scope use that.
185
- 2 . If one of the function parameters is named ` foo ` use that.
186
- 3 . If the function itself is called ` foo ` use that.
187
- 4 . Go to the next outer scope and start with ** #1 ** again.
184
+ 1 . In case there is a ` var foo ` statement in the current scope, use that.
185
+ 2 . If one of the function parameters is named ` foo ` , use that.
186
+ 3 . If the function itself is called ` foo ` , use that.
187
+ 4 . Go to the next outer scope, and start with ** #1 ** again.
188
188
189
189
> ** Note:** Having a parameter called ` arguments ` will ** prevent** the creation
190
190
> of the default ` arguments ` object.
@@ -223,7 +223,7 @@ while different in syntax, do behave the exact same way.
223
223
### In Conclusion
224
224
225
225
It is recommended to always use an * anonymous wrapper* for encapsulating code in
226
- its own namespace. This does not only protect code against name clashes, it
226
+ its own namespace. This does not only protect code against name clashes, but it
227
227
also allows for better modularization of programs.
228
228
229
229
Additionally, the use of global variables is considered ** bad practice** . ** Any**
0 commit comments