Skip to content

Commit 3306345

Browse files
committed
Update
1 parent 5681363 commit 3306345

File tree

2 files changed

+54
-55
lines changed

2 files changed

+54
-55
lines changed

index.html

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ <h1>JavaScript Garden</h1>
4949
of the language is strongly recommended in order to understand the topics covered
5050
in this guide. If you want to learn the language first, please head over to the
5151
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>
5256
</section><section><header><h3>License</h3></header>
5357
<p>JavaScript Garden is published under the <a href="https://github.com/BonsaiDen/JavaScript-Garden/blob/next/LICENSE">MIT license</a> and hosted on
5458
<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>
111115
</code></pre>
112116
<p>Object properties can be both notated as plain characters and as strings. Due to
113117
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>
116120
</section><section><header><h3>In conclusion</h3></header>
117121
<p>Objects are the bread and butter of JavaScript, nearly everything in the
118122
language is based on top of them.</p></section></article>
@@ -150,15 +154,15 @@ <h1>JavaScript Garden</h1>
150154
Bar.method()
151155
</code></pre>
152156
<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
154158
defined on <code>Foo</code>, but it will not have access to the <strong>property</strong> <code>value</code> of a
155159
<code>Foo</code> instance since that property gets defined in the <a href="#constructor">constructor</a>
156160
of <code>Foo</code>. Which, in this case, never gets called.</p>
157161
<aside>
158162
<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>
162166
</aside>
163167
</section><section><header><h3>Property lookup</h3></header>
164168
<p>When accessing the properties of an object, JavaScript will traverse the
@@ -194,9 +198,9 @@ <h1>JavaScript Garden</h1>
194198
Bar.prototype: [Foo Instance]
195199
Bar.method()
196200
</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>
200204
</section><section><header><h3>Performance</h3></header>
201205
<p>As a result of all of this this, the lookup time for properties that are high up
202206
the prototype chain can have a negative impact on performance critical code.
@@ -211,7 +215,7 @@ <h1>JavaScript Garden</h1>
211215
is still no good reason for cluttering built in types with additional
212216
non-standard functionality.</p>
213217
<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,
215219
<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach"><code>Array.forEach</code></a>.</p>
216220
</section><section><header><h3>In conclusion</h3></header>
217221
<p>It is a must to understand the prototypical inheritance model completely before
@@ -259,17 +263,17 @@ <h1>JavaScript Garden</h1>
259263
</section><section><header><h3>The <code>function</code> statement</h3></header>
260264
<pre><code>function foo() {}
261265
</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>
265269
<pre><code>foo(); // Works because foo was created before this code runs
266270
function foo() {}
267271
</code></pre>
268272
</section><section><header><h3>The <code>function</code> expression</h3></header>
269273
<pre><code>var foo = function() {};
270274
</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>
273277
<pre><code>foo; // 'undefined'
274278
foo(); // this raises a TypeError
275279
var foo = function() {};
@@ -311,7 +315,7 @@ <h1>JavaScript Garden</h1>
311315
<p>Also, while the statements inside the <code>if</code> block never get executed, the variable
312316
<code>foo</code> still gets created and defaults to the value of <code>undefined</code>.</p>
313317
</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,
315319
subsequent statements will override the previous ones.</p>
316320
<pre><code>function foo() {}
317321
var foo;
@@ -391,9 +395,9 @@ <h1>JavaScript Garden</h1>
391395
<pre><code>var test = someObject.methodTest();
392396
test();
393397
</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
397401
makes <a href="#prototype">prototypical inheritance</a> work. </p>
398402
<pre><code>function Foo() {}
399403
Foo.prototype.method = function() {};
@@ -435,7 +439,7 @@ <h1>JavaScript Garden</h1>
435439
</code></pre>
436440
<p>In the above example <code>Counter</code> returns <strong>two</strong> closures. The function <code>increment</code>
437441
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
439443
that was defined in that <strong>very</strong> scope.</p>
440444
</section><section><header><h3>Why private variables work</h3></header>
441445
<p>Since it is not possible to reference or assign scopes in JavaScript, there is
@@ -478,8 +482,7 @@ <h1>JavaScript Garden</h1>
478482
}
479483
</code></pre>
480484
<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>
483486
<p>The anonymous function that gets passed to <code>setTimeout</code> now has a reference to
484487
<code>e</code>, which value does <strong>not</strong> get changed by the loop.</p>
485488
<p>There is another possible way of achieving this. It is possible to return a
@@ -523,8 +526,8 @@ <h1>JavaScript Garden</h1>
523526

524527
<article><section><header><h2 id="scopes">Scopes and Namespaces <a href="#intro">^</a></h2></header>
525528
<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>
528531
<aside>
529532
<p><strong>Note:</strong> When not used in an assignment or as a function argument, the <code>{...}</code>
530533
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>
561564
seem like a big deal at first, but consider having a ten-thousand line
562565
JavaScript file with lots and lots of different variable names, not using <code>var</code>
563566
will introduce hard to track down bugs.</p>
564-
<p>For example, when using generic variable names like <code>i</code> in loops.</p>
565567
<pre><code>// global scope
566568
var items = [/* some list */];
567569
for(var i = 0; i &lt; 10; i++) {
@@ -577,8 +579,8 @@ <h1>JavaScript Garden</h1>
577579
</code></pre>
578580
<p>The outer loop will terminate after the first call to <code>subLoop</code> since that
579581
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>
582584
</section><section><header><h3>Local variables</h3></header>
583585
<p>The only source for local variables in JavaScript are <a href="#functions">function</a>
584586
parameters and variables that were declared with the <code>var</code> statement.</p>
@@ -755,9 +757,9 @@ <h1>JavaScript Garden</h1>
755757
<p>The above table shows the results of the type coercion and it is the main reason
756758
why the use of <code>==</code> is regarded as bad practice, it introduces hard to track down
757759
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>
761763
</section><section><header><h3>The strict equals operator</h3></header>
762764
<p>The strict equals operator consists of <strong>three</strong> equal signs: <code>===</code></p>
763765
<p>Other than the normal equals operator, the strict equals operator does <strong>not</strong>
@@ -861,15 +863,15 @@ <h1>JavaScript Garden</h1>
861863
</code></pre>
862864
</section><section><header><h3>In conclusion</h3></header>
863865
<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>
866868

867869
<article><section><header><h2 id="forinloop">The <code>for in</code> loop <a href="#intro">^</a></h2></header>
868870
<p>Just like the <code>in</code> operator, the <code>for in</code> loop also traverses the prototype
869871
chain when iterating over the properties of an object.</p>
870872
<aside>
871873
<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>
873875
property of an array.</p>
874876
</aside>
875877
<pre><code>// Poisoning Object.prototype
@@ -898,8 +900,8 @@ <h1>JavaScript Garden</h1>
898900
</code></pre>
899901
<p>This version is the only correct one to use. Due to the use of <code>hasOwnPropery</code> it
900902
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>
903905
<p>One widely used framework which does this is <a href="http://www.prototypejs.org/"><strong>Prototype</strong></a>. When this
904906
framework is included, <code>for in</code> loops that doe not use <code>hasOwnProperty</code> are
905907
guaranteed to break.</p>
@@ -942,9 +944,9 @@ <h1>JavaScript Garden</h1>
942944
you can see this is anything but consistent.</p>
943945
<p>The <em>Class</em> refers to the value of the internal <code>[[Class]]</code> property of an object.</p>
944946
<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>
948950
</aside>
949951
<p>In order to retrieve the value of <em>Class</em> one can has to make use of the
950952
<code>toString</code> method of <code>Object</code>.</p>
@@ -968,10 +970,10 @@ <h1>JavaScript Garden</h1>
968970
<code>typeof</code> is actually useful for.</p>
969971
</section><section><header><h3>In conclusion</h3></header>
970972
<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
975977
<strong>all costs</strong>.</p></section></article>
976978

977979
<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>
993995
'foo' instanceof Object; // false
994996
</code></pre>
995997
<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>
9991000
</section><section><header><h3>In conclusion</h3></header>
10001001
<p>The <code>instanceof</code> operator should <strong>only</strong> be used when dealing with custom made
10011002
objects that origin from the same JavaScript context. Just like the
@@ -1217,10 +1218,10 @@ <h1>JavaScript Garden</h1>
12171218
clearTimeout(i);
12181219
}
12191220
</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>
12241225
<p><code>setTimeout</code> and <code>setInterval</code> can also take a string as their first parameter.
12251226
This feature should <strong>never</strong> be used, since it internally makes use of <code>eval</code>.</p>
12261227
<pre><code>function foo() {
@@ -1236,8 +1237,8 @@ <h1>JavaScript Garden</h1>
12361237
bar();
12371238
</code></pre>
12381239
<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>
12411242
<p>It is further recommended to <strong>not</strong> use a string to pass arguments to the
12421243
function that will get called. </p>
12431244
<pre><code>function foo(a, b, c) {}
@@ -1309,8 +1310,6 @@ <h1>JavaScript Garden</h1>
13091310
<footer>
13101311
<p>
13111312
Copyright &copy; 2011.
1312-
Writing <a href="http://stackoverflow.com/users/170224/ivo-wetzel">Ivo Wetzel</a>.
1313-
Design &amp; Fixes <a href="http://stackoverflow.com/users/313758/yi-jiang">Zhang Yi Jiang</a>.
13141313
</p>
13151314
</footer>
13161315
</div>

js/garden.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ $(document).ready(function() {
4343
var offset = $(window).scrollTop();
4444
var id = articles[0][1];
4545
for(var i = 0, l = articles.length; i < l; i++) {
46-
if (articles[i][0] < offset + 10) {
46+
if (articles[i][0] < offset + 200) {
4747
id = articles[i][1];
4848
}
4949
}
5050
selectNavigation(id, offset === 0);
5151
}
5252

53-
var initSelection = setTimeout(highlightSection, 50);
53+
var initSelection = setTimeout(highlightSection, 100);
5454
$(document).scroll(function() {
5555
clearTimeout(initSelection);
5656
highlightSection();

0 commit comments

Comments
 (0)