11## Scopes and namespaces
22
3- Although JavaScript deals fine with the block syntax of two matching curly
4- braces, it does ** not** support block scope; thus , all that is left is the
5- * function scope* .
3+ Although JavaScript deals fine with the syntax of two matching curly
4+ braces for blocks , it does ** not** support block scope; hence , all that is left
5+ is in the language is * function scope* .
66
77 function test() { // a scope
88 for(var i = 0; i < 10; i++) { // not a scope
@@ -11,13 +11,13 @@ braces, it does **not** support block scope; thus, all that is left is the
1111 console.log(i); // 10
1212 }
1313
14- > ** Note:** When not used in an assignment or as a function argument, the ` {...} `
15- > notation will get interpreted as a block statement and ** not ** as an ` Object ` .
16- > This, in conjunction with [ automatic insertion of semicolons ] ( #semicolon ) ,
17- > can lead to subtle errors.
14+ > ** Note:** When not used in an assignment, return statement or as a function
15+ > argument, the ` {...} ` notation will get interpreted as a block statement and
16+ > ** not ** as an object literal. This, in conjunction with
17+ > [ automatic insertion of semicolons ] ( #semicolon ) , can lead to subtle errors.
1818
19- Additionally, there are no distinct namespaces in JavaScript. This means that
20- everything gets defined in ** one** globally shared namespace.
19+ There are also no distinct namespaces in JavaScript. This means that everything
20+ gets defined in ** one** globally shared namespace.
2121
2222Each time a variable is referenced, JavaScript will traverse upwards through all
2323the scopes until it finds it. In the case that it reaches the global scope and
@@ -47,10 +47,10 @@ implications.
4747 test();
4848 foo; // 21
4949
50- Leaving out the ` var ` statement will override the value of ` foo ` , this might not
51- seem like a big deal at first, but consider having a ten-thousand line
52- JavaScript file with lots and lots of different variable names, not using ` var `
53- will introduce hard to track down bugs.
50+ Leaving out the ` var ` statement inside the function ` test ` will override the
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 hard to
53+ track down bugs.
5454
5555 // global scope
5656 var items = [ /* some list * /] ;
@@ -65,15 +65,15 @@ will introduce hard to track down bugs.
6565 }
6666 }
6767
68- The outer loop will terminate after the first call to ` subLoop ` since that
69- function overwrites the global value of ` i ` . Using a ` var ` for the second
70- ` for ` loop would have easily avoided this error. The ` var ` statement should never
71- be left out unless the desired effect ** is** to affect the outer scope.
68+ The outer loop will terminate after the first call to ` subLoop ` , since ` subLoop `
69+ overwrites the global value of ` i ` . Using a ` var ` for the second ` for ` loop would
70+ have easily avoided this error. The ` var ` statement should never be left out
71+ unless the desired effect ** is** to affect the outer scope.
7272
7373### Local variables
7474
7575The only source for local variables in JavaScript are [ function] ( #functions )
76- parameters and variables that were declared with the ` var ` statement.
76+ parameters and variables that were declared via the ` var ` statement.
7777
7878 // global scope
7979 var foo = 1;
@@ -90,7 +90,7 @@ parameters and variables that were declared with the `var` statement.
9090 test(10);
9191
9292While ` foo ` and ` i ` are local variables inside the scope of the function ` test ` ,
93- the assignment of ` bar ` will override the global variable.
93+ the assignment of ` bar ` will override the global variable with the same name .
9494
9595### Name resolution order
9696
@@ -106,16 +106,16 @@ function, JavaScript will lookup the name in the following order:
106106 1 . In case there's a ` var foo ` statement in the current scope use that.
107107 2 . If one of the function parameters is named ` foo ` use that.
108108 3 . If the function itself is called ` foo ` use that.
109- 4 . Go to the next outer scope and start from ** #1 ** again.
109+ 4 . Go to the next outer scope and start with ** #1 ** again.
110110
111111> ** Note:** Having a parameter called ` arguments ` will ** override** the default
112112> ` arguments ` object.
113113
114114### Namespaces
115115
116- A common problem of having only one global namespace is that it is very easy to
117- run into problems where variable names clash. But this can be easily avoided
118- with the help of anonymous * function wrappers* .
116+ A common problem of having only one global namespace is the likeliness of running
117+ into problems where variable names clash. In JavaScript, this problem can be
118+ easily avoided with the help of anonymous * function wrappers* .
119119
120120 (function() {
121121 // a self contained "namespace"
@@ -127,16 +127,15 @@ with the help of anonymous *function wrappers*.
127127 })(); // execute the function immediately
128128
129129
130- Since a unnamed functions are not considered as [ statement] ( #functions ) , they
131- get interpreted as expression, in order to run those functions they must fist be
132- evaluate and then called.
130+ Unnamed functions are considered [ expressions] ( #functions ) ; so in order to being
131+ callable, they must first be evaluated.
133132
134133 ( // evaluate the function inside the paranthesis
135134 function() {}
136135 ) // and return the function object
137136 () // call the result of the evaluation
138137
139- There are other ways for evaluating and calling the function expression; which
138+ There are other ways for evaluating and calling the function expression; which,
140139while different in syntax, do the exact same thing.
141140
142141 // Two other ways
@@ -146,10 +145,9 @@ while different in syntax, do the exact same thing.
146145### In conclusion
147146
148147It is recommended to always use an * anonymous wrapper* for encapsulating code in
149- its own namespace. This does not only protect against name clashes, but it also
150- allows for better modularization.
148+ its own namespace. This does not only protect the code against name clashes, it
149+ also allows for better modularization.
151150
152151Additionally, the use of global variables is considered ** bad practice** , any use
153- of them indicates badly written code that is prone to failure and hard to
154- maintain.
152+ of them indicates badly written code that is prone to errors and hard to maintain.
155153
0 commit comments