@@ -49,6 +49,10 @@ <h1>JavaScript Garden</h1>
49
49
of the language is strongly recommended in order to understand the topics covered
50
50
in this guide. If you want to learn the language first, please head over to the
51
51
excellent < a href ="https://developer.mozilla.org/en/JavaScript/Guide "> Guide</ a > on the Mozilla Developer Network.</ p >
52
+ < p > JavaScript Garden also focuses on the 3rd Edition of the EcmaScript </ p >
53
+ </ section > < section > < header > < h3 > The authors</ h3 > </ header >
54
+ < p > This guide is the work of two lovely Stackoverflow users, < a href ="http://stackoverflow.com/users/170224/ivo-wetzel "> Ivo Wetzel</ a >
55
+ (Writing) and < a href ="http://stackoverflow.com/users/313758/yi-jiang "> Zhang Yi Jiang</ a > (Design).</ p >
52
56
</ section > < section > < header > < h3 > License</ h3 > </ header >
53
57
< p > JavaScript Garden is published under the < a href ="https://github.com/BonsaiDen/JavaScript-Garden/blob/next/LICENSE "> MIT license</ a > and hosted on
54
58
< a href ="https://github.com/BonsaiDen/JavaScript-Garden "> GitHub</ a > . If you find errors or typos please < a href ="https://github.com/BonsaiDen/JavaScript-Garden/issues "> file an issue</ a > or a pull
@@ -111,8 +115,8 @@ <h1>JavaScript Garden</h1>
111
115
</ code > </ pre >
112
116
< p > Object properties can be both notated as plain characters and as strings. Due to
113
117
another mis-design in JavaScript's parser, the above raises a < code > SyntaxError</ code > .</ p >
114
- < p > The error is getting raised because < code > delete</ code > is a < em > keyword</ em > of the language,
115
- therefore it must be, just like < code > case </ code > , notated as a string literal.</ p >
118
+ < p > The error is getting raised because < code > delete</ code > is a < em > keyword</ em > of the language;
119
+ therefore, it must be notated as a string literal.</ p >
116
120
</ section > < section > < header > < h3 > In conclusion</ h3 > </ header >
117
121
< p > Objects are the bread and butter of JavaScript, nearly everything in the
118
122
language is based on top of them.</ p > </ section > </ article >
@@ -150,15 +154,15 @@ <h1>JavaScript Garden</h1>
150
154
Bar.method()
151
155
</ code > </ pre >
152
156
< p > The above object < code > test</ code > will inherit from both < code > Bar.prototype</ code > and
153
- < code > Foo.prototype</ code > . It will therefore have access to the function < code > method</ code > that was
157
+ < code > Foo.prototype</ code > . Hence, it will have access to the function < code > method</ code > that was
154
158
defined on < code > Foo</ code > , but it will not have access to the < strong > property</ strong > < code > value</ code > of a
155
159
< code > Foo</ code > instance since that property gets defined in the < a href ="#constructor "> constructor</ a >
156
160
of < code > Foo</ code > . Which, in this case, never gets called.</ p >
157
161
< aside >
158
162
< p > < strong > Note:</ strong > Do < strong > not</ strong > use < code > Bar.property = Foo</ code > , since it will not point to
159
- the prototype of < code > Foo</ code > but rather to the function object < code > Foo</ code > . Therefore the
160
- prototype chain will go over < code > Function.prototype</ code > in this case , which results
161
- in < code > method</ code > not being on the prototype chain .</ p >
163
+ the prototype of < code > Foo</ code > but rather to the function object < code > Foo</ code > . Because of
164
+ that, the prototype chain will go over < code > Function.prototype</ code > , which results
165
+ in < code > method</ code > not being on it .</ p >
162
166
</ aside >
163
167
</ section > < section > < header > < h3 > Property lookup</ h3 > </ header >
164
168
< p > When accessing the properties of an object, JavaScript will traverse the
@@ -194,9 +198,9 @@ <h1>JavaScript Garden</h1>
194
198
Bar.prototype: [Foo Instance]
195
199
Bar.method()
196
200
</ code > </ pre >
197
- < p > Now < code > Bar.prototype</ code > points to an < em > instance</ em > of < code > Foo</ code > , therefore the property
198
- < code > value</ code > of < strong > that</ strong > instance is now on the prototype chain and since < code > Foo</ code > itself
199
- has a prototype, the chain continues with that one afterwards.</ p >
201
+ < p > Now < code > Bar.prototype</ code > points to an < em > instance</ em > of < code > Foo</ code > ; thus, the property
202
+ < code > value</ code > of that very instance is now on the prototype chain. And since < code > Foo</ code >
203
+ itself has a prototype, the chain continues with that one afterwards.</ p >
200
204
</ section > < section > < header > < h3 > Performance</ h3 > </ header >
201
205
< p > As a result of all of this this, the lookup time for properties that are high up
202
206
the prototype chain can have a negative impact on performance critical code.
@@ -211,7 +215,7 @@ <h1>JavaScript Garden</h1>
211
215
is still no good reason for cluttering built in types with additional
212
216
non-standard functionality.</ p >
213
217
< p > The < strong > only</ strong > good reason for extending a built in prototype is to back port
214
- the features of newer JavaScript engines to older ones, like for example
218
+ the features of newer JavaScript engines to older ones; for example,
215
219
< a href ="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach "> < code > Array.forEach</ code > </ a > .</ p >
216
220
</ section > < section > < header > < h3 > In conclusion</ h3 > </ header >
217
221
< p > It is a must to understand the prototypical inheritance model completely before
@@ -259,17 +263,17 @@ <h1>JavaScript Garden</h1>
259
263
</ section > < section > < header > < h3 > The < code > function</ code > statement</ h3 > </ header >
260
264
< pre > < code > function foo() {}
261
265
</ code > </ pre >
262
- < p > The above function gets created < strong > before</ strong > the execution of the program starts.
263
- Therefore it is available everywhere in the scope it was < em > defined</ em > in, even if it
264
- is called before its actual definition in the source.</ p >
266
+ < p > The above function gets created < strong > before</ strong > the execution of the program starts;
267
+ thus, it is available < em > everywhere</ em > in the scope it was < em > defined</ em > in, even if
268
+ called before the actual definition in the source.</ p >
265
269
< pre > < code > foo(); // Works because foo was created before this code runs
266
270
function foo() {}
267
271
</ code > </ pre >
268
272
</ section > < section > < header > < h3 > The < code > function</ code > expression</ h3 > </ header >
269
273
< pre > < code > var foo = function() {};
270
274
</ code > </ pre >
271
- < p > The above assigns the unnamed and therefore < em > anonymous</ em > function, to the variable
272
- < code > foo</ code > . </ p >
275
+ < p > The above assigns the unnamed and, therefore, < em > anonymous</ em > function, to the
276
+ variable < code > foo</ code > . </ p >
273
277
< pre > < code > foo; // 'undefined'
274
278
foo(); // this raises a TypeError
275
279
var foo = function() {};
@@ -311,7 +315,7 @@ <h1>JavaScript Garden</h1>
311
315
< p > Also, while the statements inside the < code > if</ code > block never get executed, the variable
312
316
< code > foo</ code > still gets created and defaults to the value of < code > undefined</ code > .</ p >
313
317
</ section > < section > < header > < h3 > < code > var</ code > vs. < code > function</ code > </ h3 > </ header >
314
- < p > All < code > var</ code > statements get parsed < strong > before</ strong > < code > function</ code > statements, therefore
318
+ < p > All < code > var</ code > statements get parsed < strong > before</ strong > < code > function</ code > statements; hence,
315
319
subsequent statements will override the previous ones.</ p >
316
320
< pre > < code > function foo() {}
317
321
var foo;
@@ -391,9 +395,9 @@ <h1>JavaScript Garden</h1>
391
395
< pre > < code > var test = someObject.methodTest();
392
396
test();
393
397
</ code > </ pre >
394
- < p > Again due to the first case, < code > test</ code > now acts like like a plain function call and
395
- therefore the < code > this</ code > inside it will no longer refer to < code > someObject</ code > .</ p >
396
- < p > While the late binding of < code > this</ code > might seem like a bad thing , it is fact what
398
+ < p > Again due to the first case < code > test</ code > now acts like like a plain function call;
399
+ therefore, < code > this</ code > inside it will no longer refer to < code > someObject</ code > .</ p >
400
+ < p > While the late binding of < code > this</ code > might seem like a bad idea , it is in fact what
397
401
makes < a href ="#prototype "> prototypical inheritance</ a > work. </ p >
398
402
< pre > < code > function Foo() {}
399
403
Foo.prototype.method = function() {};
@@ -435,7 +439,7 @@ <h1>JavaScript Garden</h1>
435
439
</ code > </ pre >
436
440
< p > In the above example < code > Counter</ code > returns < strong > two</ strong > closures. The function < code > increment</ code >
437
441
as well as the function < code > get</ code > . Both of these functions keep a < strong > reference</ strong > to
438
- the scope of < code > Counter</ code > and therefore always have access to the < code > count</ code > variable
442
+ the scope of < code > Counter</ code > and, therefore, always have access to the < code > count</ code > variable
439
443
that was defined in that < strong > very</ strong > scope.</ p >
440
444
</ section > < section > < header > < h3 > Why private variables work</ h3 > </ header >
441
445
< p > Since it is not possible to reference or assign scopes in JavaScript, there is
@@ -478,8 +482,7 @@ <h1>JavaScript Garden</h1>
478
482
}
479
483
</ code > </ pre >
480
484
< p > The anonymous outer function gets called immediately with < code > i</ code > as the first
481
- argument, therefore it will receive a copy of the < strong > value</ strong > of < code > i</ code > as its
482
- parameter < code > e</ code > .</ p >
485
+ argument and will receive a copy of the < strong > value</ strong > of < code > i</ code > as its parameter < code > e</ code > .</ p >
483
486
< p > The anonymous function that gets passed to < code > setTimeout</ code > now has a reference to
484
487
< code > e</ code > , which value does < strong > not</ strong > get changed by the loop.</ p >
485
488
< p > There is another possible way of achieving this. It is possible to return a
@@ -523,8 +526,8 @@ <h1>JavaScript Garden</h1>
523
526
524
527
< article > < section > < header > < h2 id ="scopes "> Scopes and Namespaces < a href ="#intro "> ^</ a > </ h2 > </ header >
525
528
< p > Although JavaScript deals fine with the block scope syntax of two matching curly
526
- braces, it does < strong > not</ strong > support block scope. Therefore all that's left is < em > function
527
- scope</ em > .</ p >
529
+ braces, it does < strong > not</ strong > support block scope; thus, all that is left is the
530
+ < em > function scope</ em > .</ p >
528
531
< aside >
529
532
< p > < strong > Note:</ strong > When not used in an assignment or as a function argument, the < code > {...}</ code >
530
533
notation will get interpreted as a block statement and < strong > not</ strong > as an < code > Object</ code > .
@@ -561,7 +564,6 @@ <h1>JavaScript Garden</h1>
561
564
seem like a big deal at first, but consider having a ten-thousand line
562
565
JavaScript file with lots and lots of different variable names, not using < code > var</ code >
563
566
will introduce hard to track down bugs.</ p >
564
- < p > For example, when using generic variable names like < code > i</ code > in loops.</ p >
565
567
< pre > < code > // global scope
566
568
var items = [/* some list */];
567
569
for(var i = 0; i < 10; i++) {
@@ -577,8 +579,8 @@ <h1>JavaScript Garden</h1>
577
579
</ code > </ pre >
578
580
< p > The outer loop will terminate after the first call to < code > subLoop</ code > since that
579
581
function overwrites the global value of < code > i</ code > . Using a < code > var</ code > for the second
580
- < code > for</ code > loop would have easily avoided this error, therefore < code > var</ code > should never be
581
- left out unless the desired effect < strong > is</ strong > to affect the outer scope.</ p >
582
+ < code > for</ code > loop would have easily avoided this error. The < code > var</ code > statement should never
583
+ be left out unless the desired effect < strong > is</ strong > to affect the outer scope.</ p >
582
584
</ section > < section > < header > < h3 > Local variables</ h3 > </ header >
583
585
< p > The only source for local variables in JavaScript are < a href ="#functions "> function</ a >
584
586
parameters and variables that were declared with the < code > var</ code > statement.</ p >
@@ -755,9 +757,9 @@ <h1>JavaScript Garden</h1>
755
757
< p > The above table shows the results of the type coercion and it is the main reason
756
758
why the use of < code > ==</ code > is regarded as bad practice, it introduces hard to track down
757
759
bugs due to its complicated conversion rules.</ p >
758
- < p > Additionally there is also a performance impact when type coercion is in play,
759
- since for example a string has to be converted to a number before the comparison
760
- is done. </ p >
760
+ < p > Additionally there is also a performance impact when type coercion is in play;
761
+ for example, a string has to be converted to a number before it can be compared
762
+ with another number. </ p >
761
763
</ section > < section > < header > < h3 > The strict equals operator</ h3 > </ header >
762
764
< p > The strict equals operator consists of < strong > three</ strong > equal signs: < code > ===</ code > </ p >
763
765
< p > Other than the normal equals operator, the strict equals operator does < strong > not</ strong >
@@ -861,15 +863,15 @@ <h1>JavaScript Garden</h1>
861
863
</ code > </ pre >
862
864
</ section > < section > < header > < h3 > In conclusion</ h3 > </ header >
863
865
< p > The use of the < code > Array</ code > constructor should be avoided as much as possible. The < code > []</ code >
864
- notation is definitely preferred. It is shorter and has a clear syntax, and is
865
- therefore increasing the readability of code and helps to avoid subtle mistakes .</ p > </ section > </ article >
866
+ notation is definitely preferred. It is shorter and has a clearer syntax; thus,
867
+ it also increases the readability of code.</ p > </ section > </ article >
866
868
867
869
< article > < section > < header > < h2 id ="forinloop "> The < code > for in</ code > loop < a href ="#intro "> ^</ a > </ h2 > </ header >
868
870
< p > Just like the < code > in</ code > operator, the < code > for in</ code > loop also traverses the prototype
869
871
chain when iterating over the properties of an object.</ p >
870
872
< aside >
871
873
< p > < strong > Note:</ strong > The < code > for in</ code > loop will < strong > not</ strong > iterate over any properties that
872
- have their < code > enumerable</ code > attribute set to < code > false</ code > , for example the < code > length</ code >
874
+ have their < code > enumerable</ code > attribute set to < code > false</ code > ; for example, the < code > length</ code >
873
875
property of an array.</ p >
874
876
</ aside >
875
877
< pre > < code > // Poisoning Object.prototype
@@ -898,8 +900,8 @@ <h1>JavaScript Garden</h1>
898
900
</ code > </ pre >
899
901
< p > This version is the only correct one to use. Due to the use of < code > hasOwnPropery</ code > it
900
902
will < strong > only</ strong > print out < code > moo</ code > . When < code > hasOwnProperty</ code > is left out, the code is
901
- prone to errors when the native prototypes - for example < code > Object.prototype </ code > -
902
- have been extended .</ p >
903
+ prone to errors when the native prototypes have been extended; for example,
904
+ < code > Object.prototype </ code > .</ p >
903
905
< p > One widely used framework which does this is < a href ="http://www.prototypejs.org/ "> < strong > Prototype</ strong > </ a > . When this
904
906
framework is included, < code > for in</ code > loops that doe not use < code > hasOwnProperty</ code > are
905
907
guaranteed to break.</ p >
@@ -942,9 +944,9 @@ <h1>JavaScript Garden</h1>
942
944
you can see this is anything but consistent.</ p >
943
945
< p > The < em > Class</ em > refers to the value of the internal < code > [[Class]]</ code > property of an object.</ p >
944
946
< aside >
945
- < p > < strong > From the Specification:</ strong > < em > Class</ em > can be one of the following values:
946
- < code > " Arguments" </ code > , < code > " Array" </ code > , < code > " Boolean" </ code > , < code > " Date" </ code > , < code > " Error" </ code > , < code > "Function" </ code > ,
947
- < code > " JSON" </ code > , < code > " Math" </ code > , < code > " Number" </ code > , < code > " Object" </ code > , < code > " RegExp" </ code > , < code > " String" </ code > </ p >
947
+ < p > < strong > From the Specification:</ strong > The value of < code > [[ Class]] </ code > can be one of the
948
+ following strings. < code > Arguments</ code > , < code > Array</ code > , < code > Boolean</ code > , < code > Date</ code > , < code > Error</ code > ,
949
+ < code > Function </ code > , < code > JSON</ code > , < code > Math</ code > , < code > Number</ code > , < code > Object</ code > , < code > RegExp</ code > , < code > String</ code > . </ p >
948
950
</ aside >
949
951
< p > In order to retrieve the value of < em > Class</ em > one can has to make use of the
950
952
< code > toString</ code > method of < code > Object</ code > .</ p >
@@ -968,10 +970,10 @@ <h1>JavaScript Garden</h1>
968
970
< code > typeof</ code > is actually useful for.</ p >
969
971
</ section > < section > < header > < h3 > In conclusion</ h3 > </ header >
970
972
< p > In order to check the type of an object, it is highly recommended to use
971
- < code > Object.prototype.toString</ code > , as it is the only reliable way of doing so.
972
- As shown in the type table, some return values of < code > typeof</ code > are not defined in the
973
- specification and can therefore differ in various implementations.</ p >
974
- < p > Unless checking for a variable to be defined, < code > typeof</ code > should be avoided at
973
+ < code > Object.prototype.toString</ code > ; as this is the only reliable way of doing so.
974
+ As shown in the above type table, some return values of < code > typeof</ code > are not defined
975
+ in the specification; thus, the can across various implementations.</ p >
976
+ < p > Unless checking whether a variable is defined, < code > typeof</ code > should be avoided at
975
977
< strong > all costs</ strong > .</ p > </ section > </ article >
976
978
977
979
< article > < section > < header > < h2 id ="instanceof "> The < code > instanceof</ code > operator < a href ="#intro "> ^</ a > </ h2 > </ header >
@@ -993,9 +995,8 @@ <h1>JavaScript Garden</h1>
993
995
'foo' instanceof Object; // false
994
996
</ code > </ pre >
995
997
< p > One important thing to note is that < code > instanceof</ code > does of course not work on
996
- objects that origin from different JavaScript contexts. For example: Different
997
- documents in a web browser.
998
- a Web Browser.</ p >
998
+ objects that origin from different JavaScript contexts;rFor example, different
999
+ documents in a web browser.</ p >
999
1000
</ section > < section > < header > < h3 > In conclusion</ h3 > </ header >
1000
1001
< p > The < code > instanceof</ code > operator should < strong > only</ strong > be used when dealing with custom made
1001
1002
objects that origin from the same JavaScript context. Just like the
@@ -1217,10 +1218,10 @@ <h1>JavaScript Garden</h1>
1217
1218
clearTimeout(i);
1218
1219
}
1219
1220
</ code > </ pre >
1220
- < p > There might still be timeouts that are unaffected by this arbitrary number, it
1221
- is therefore recommended to keep track of all the timeouts and intervals IDs
1222
- instead .</ p >
1223
- </ section > < section > < header > < h3 > Things to avoid at all costs </ h3 > </ header >
1221
+ < p > There might still be timeouts that are unaffected by this arbitrary number;
1222
+ therefore, is is instead recommended to keep track of all the timeout IDs, so
1223
+ they can be cleared one by one .</ p >
1224
+ </ section > < section > < header > < h3 > Hidden < code > eval </ code > magic </ h3 > </ header >
1224
1225
< p > < code > setTimeout</ code > and < code > setInterval</ code > can also take a string as their first parameter.
1225
1226
This feature should < strong > never</ strong > be used, since it internally makes use of < code > eval</ code > .</ p >
1226
1227
< pre > < code > function foo() {
@@ -1236,8 +1237,8 @@ <h1>JavaScript Garden</h1>
1236
1237
bar();
1237
1238
</ code > </ pre >
1238
1239
< p > Since < code > eval</ code > is not getting < a href ="#eval "> called directly</ a > here, the string passed to
1239
- < code > setTimeout</ code > will get executed in the global scope and will therefore not use
1240
- the local < code > foo</ code > of < code > bar</ code > .</ p >
1240
+ < code > setTimeout</ code > will get executed in the global scope; thus, it will not use the
1241
+ local variable < code > foo</ code > from the scope of < code > bar</ code > .</ p >
1241
1242
< p > It is further recommended to < strong > not</ strong > use a string to pass arguments to the
1242
1243
function that will get called. </ p >
1243
1244
< pre > < code > function foo(a, b, c) {}
@@ -1309,8 +1310,6 @@ <h1>JavaScript Garden</h1>
1309
1310
< footer >
1310
1311
< p >
1311
1312
Copyright © 2011.
1312
- Writing < a href ="http://stackoverflow.com/users/170224/ivo-wetzel "> Ivo Wetzel</ a > .
1313
- Design & Fixes < a href ="http://stackoverflow.com/users/313758/yi-jiang "> Zhang Yi Jiang</ a > .
1314
1313
</ p >
1315
1314
</ footer >
1316
1315
</ div >
0 commit comments