Skip to content

Commit 6cc502e

Browse files
committed
Yes, we are working on it.
1 parent e3cef12 commit 6cc502e

40 files changed

+2594
-327
lines changed

doc/de/index.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"title": "JavaScript Garden",
3+
"langTitle": "JavaScript Garden auf Deutsch",
4+
"description": "Ein Guide zu JavaScript Ecken und Kanten.",
5+
"sections": [
6+
{
7+
"title": "Einführung",
8+
"dir": "intro",
9+
"articles": [
10+
"authors",
11+
"contributors",
12+
"license"
13+
]
14+
}
15+
]
16+
}

doc/de/intro/authors.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## The Authors
2+
3+
This guide is the work of two lovely Stack Overflow users, [Ivo Wetzel][1]
4+
(Writing) and [Zhang Yi Jiang][2] (Design).
5+
6+
In case you are interested in additional guidance or reviews concerning your JavaScript
7+
projects, Ivo Wetzel offers these on a freelance basis. Please feel free to
8+
contact him via [e-mail][3] for further details.
9+
10+
[1]: http://stackoverflow.com/users/313758/yi-jiang
11+
[2]: http://stackoverflow.com/users/170224/ivo-wetzel
12+
[3]: mailto:[email protected]
13+

doc/de/intro/contributors.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Contributors
2+
3+
- [Caio Romão][1] (Spelling corrections)
4+
- [Andreas Blixt][2] (Language corrections)
5+
6+
[1]: https://github.com/caio
7+
[2]: https://github.com/blixt
8+

doc/de/intro/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Einführung
2+
3+
Übersetzen.
4+

doc/de/intro/license.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## License
2+
3+
JavaScript Garden is published under the [MIT license][1] and hosted on
4+
[GitHub][2]. If you find errors or typos please [file an issue][3] or a pull
5+
request on the repository. You can also find us in the [JavaScript room][4] on
6+
Stack Overflow chat.
7+
8+
[1]: https://github.com/BonsaiDen/JavaScript-Garden/blob/next/LICENSE
9+
[2]: https://github.com/BonsaiDen/JavaScript-Garden
10+
[3]: https://github.com/BonsaiDen/JavaScript-Garden/issues
11+
[4]: http://chat.stackoverflow.com/rooms/17/javascript
12+

doc/en/array/constructor.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## The `Array` Constructor
2+
3+
Since the `Array` constructor is ambiguous in how it deals with its parameters,
4+
it is recommended to always use the `[]` notation when creating new arrays.
5+
6+
[1, 2, 3]; // Result: [1, 2, 3]
7+
new Array(1, 2, 3); // Result: [1, 2, 3]
8+
9+
[3]; // Result: [3]
10+
new Array(3); // Result: [undefined, undefined, undefined]
11+
new Array('3') // Result: ['3']
12+
13+
In cases when there is only one argument being passed to the `Array` constructor,
14+
and that argument is a `Number`, the constructor will use that number as the
15+
*length* of the new array to be created.
16+
17+
This behavior only comes in handy in a few cases, like repeating a string, in
18+
which it avoids the use of a `for` loop.
19+
20+
new Array(count + 1).join(stringToRepeat);
21+
22+
### In Conclusion
23+
24+
The use of the `Array` constructor should be avoided as much as possible. The `[]`
25+
notation is definitely preferred. It is shorter and has a clearer syntax; thus,
26+
it also increases the readability of code.
27+

doc/en/array/general.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Array Iteration and Properties
2+
3+
Although arrays in JavaScript are objects, there are no good reasons to use
4+
the [for in loop](#object.forinloop) in for iteration on them. In fact there are
5+
a number of good reasons **against** the use of `for in` on arrays.
6+
7+
> **Note:** JavaScript arrays are **not** *associative arrays*. JavaScript only
8+
> has [objects](#object.general) for mapping keys to values. And while associative
9+
> arrays **preserve** order, objects do **not**.
10+
11+
Since the `for in` loop enumerates all properties on the prototype chain and
12+
the only way to exclude those properties is to use
13+
[`hasOwnProperty`](#object.hasownproperty), it is already up to **twenty times**
14+
slower than a normal `for` loop.
15+
16+
### Iteration
17+
18+
In order to achieve the best performance when iterating over arrays, it is best
19+
to use the classic `for` loop.
20+
21+
var list = [1, 2, 3, 4, 5, ...... 100000000];
22+
for(var i = 0, l = list.length; i < l; i++) {
23+
console.log(list[i]);
24+
}
25+
26+
There is one extra catch in the above example, which is the caching of the
27+
length of the array via `l = list.length`.
28+
29+
Although the `length` property is defined on the array itself, there is still an
30+
overhead for doing the lookup on each iteration of the loop. And while recent
31+
JavaScript engines **may** apply optimization in this case, there is no way of
32+
telling whether the code will run on one of these newer engines or not.
33+
34+
In fact, leaving out the caching may result in the loop being only **half as
35+
fast** as with the cached length.
36+
37+
### The `length` Property
38+
39+
While the *getter* of the `length` property simply returns the number of
40+
elements that are contained in the array, the *setter* can be used to
41+
**truncate** the array.
42+
43+
var foo = [1, 2, 3, 4, 5, 6];
44+
foo.length = 3;
45+
foo; // [1, 2, 3]
46+
47+
foo.length = 6;
48+
foo; // [1, 2, 3]
49+
50+
Assigning a smaller length does truncate the array, but increasing the length
51+
does not have any effect on the array.
52+
53+
### In Conclusion
54+
55+
For the best performance it is recommended to always use the plain `for` loop
56+
and cache the `length` property. The use of `for in` on an array is a sign of
57+
badly written code that is prone to bugs and bad performance. Additionally,
58+
never should any assumptions be made whether the JavaScript engine will apply
59+
optimization to the code or not.
60+

doc/en/core/eval.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## The Evil `eval`
2+
3+
The `eval` function will execute a string of JavaScript code in the local scope.
4+
5+
var foo = 1;
6+
function test() {
7+
var foo = 2;
8+
eval('foo = 3');
9+
return foo;
10+
}
11+
test(); // 3
12+
foo; // 1
13+
14+
But `eval` only executes in local scope when it is being called directly **and**
15+
the name of the function that was actually called is `eval`.
16+
17+
var foo = 1;
18+
function test() {
19+
var foo = 2;
20+
var bar = eval;
21+
bar('foo = 3');
22+
return foo;
23+
}
24+
test(); // 2
25+
foo; // 3
26+
27+
The use of `eval` should be avoided at **all costs**. 99.9% of its "uses" can be
28+
achieved **without** it.
29+
30+
### `eval` in Disguise
31+
32+
The [timeout functions](#other.timeouts) `setTimeout` and `setInterval` can both
33+
take a string as their first argument. This string will **always** get executed
34+
in the global scope since `eval` is not being called directly in that case.
35+
36+
### Security Issues
37+
38+
Also, `eval` is a security problem as it executes **any** code given to it,
39+
it should be **NEVER** used with strings of unknown or untrusted origins.
40+
41+
### In Conclusion
42+
43+
`eval` is **EVIL**. It should never be used, any code that makes use of it is to
44+
be questioned in its workings and security. If something requires `eval` in
45+
order to work, it is to be considered as magic and should **not** be used in the
46+
first place, a **better** design should be used that does not require the use of
47+
`eval`.
48+

doc/en/core/semicolon.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
## Automatic semicolon insertion
2+
3+
Although JavaScript has C style syntax, it does **not** enforce the use of
4+
semicolons in the source code, it is possible to omit them.
5+
6+
But JavaScript is not a semicolon-less language, it in fact needs the
7+
semicolons in order to understand the source code. Therefore the JavaScript
8+
parser **automatically** inserts them whenever it encounters a parse
9+
error due to a missing semicolon.
10+
11+
var foo = function() {
12+
} // parse error, semicolon expected
13+
test()
14+
15+
Insertion happens, and the parser tries again.
16+
17+
var foo = function() {
18+
}; // no error, parser continues
19+
test()
20+
21+
The automatic insertion of semicolon is considered to be one of **biggest**
22+
design flaws in the language as it *can* change the behavior of code.
23+
24+
### How it works
25+
26+
The code below has no semicolons in it, so it is up to the parser to decide where
27+
to insert them.
28+
29+
(function(window, undefined) {
30+
function test(options) {
31+
log('testing!')
32+
33+
(options.list || []).forEach(function(i) {
34+
35+
})
36+
37+
options.value.test(
38+
'long string to pass here',
39+
'and another long string to pass'
40+
)
41+
42+
return
43+
{
44+
foo: function() {}
45+
}
46+
}
47+
window.test = test
48+
49+
})(window)
50+
51+
(function(window) {
52+
window.someLibrary = {}
53+
54+
})(window)
55+
56+
Below is the result of the parser's "guessing" game.
57+
58+
(function(window, undefined) {
59+
function test(options) {
60+
61+
// Not inserted, lines got merged
62+
log('testing!')(options.list || []).forEach(function(i) {
63+
64+
}); // <- inserted
65+
66+
options.value.test(
67+
'long string to pass here',
68+
'and another long string to pass'
69+
); // <- inserted
70+
71+
return; <- inserted, breaks the return statement
72+
{ // treated as a block
73+
74+
// a label and a single expression statement
75+
foo: function() {}
76+
}; // <- inserted
77+
}
78+
window.test = test; // <- inserted
79+
80+
// The lines got merged again
81+
})(window)(function(window) {
82+
window.someLibrary = {}; //<- inserted
83+
84+
})(window); //<- inserted
85+
86+
> **Note:** The JavaScript parser does not "correctly" handle return statements
87+
> which are followed by a new line, while this is not neccessarily the fault of
88+
> the automatic semicolon insertion, it can still be an unwanted side-effect.
89+
90+
The parser drastically changed the behavior of the code above, in certain cases
91+
it does the **wrong** thing.
92+
93+
### Leading parenthesis
94+
95+
In case of a leading parenthesis, the parse will **not** insert a semicolon.
96+
97+
log('testing!')
98+
(options.list || []).forEach(function(i) {})
99+
100+
This code gets transformed into one line.
101+
102+
log('testing!')(options.list || []).forEach(function(i) {})
103+
104+
Chances are **very** high that `log` does **not** return a function, therefore the
105+
above will yield `TypeError` saying that `undefined is not a function`.
106+
107+
### In conclusion
108+
109+
It is highly recommended to **never** omit semicolons, it is also advocated to
110+
keep braces on the same line with their corresponding statements and to never omit
111+
them for one single-line `if` / `else` statements. Both of these measures will
112+
not only improve the consistency of the code, they will also prevent the
113+
JavaScript parser from changing its behavior.
114+

doc/en/core/undefined.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
## `undefined` and `null`
2+
3+
JavaScript has two distinct values for "nothing", the more useful of those two
4+
being `undefined`.
5+
6+
### The value `undefined`
7+
8+
`undefined` is a type with exactly one value: `undefined`.
9+
10+
The language also defines a global variable that has the value of `undefined`,
11+
this variable is also called `undefined`. But this variable is **not** a constant,
12+
meaning that it can be easily overwritten which then leads to abstruse bugs.
13+
14+
> **ES5 Note:** `undefined` in ECMAScript 5 is **no longer** *writable* in strict
15+
> mode, but its name can still be shadowed by for example a function with the name
16+
> `undefined`.
17+
18+
Some examples for when the value `undefined` is returned:
19+
20+
- Accessing the (unmodified) global variable `undefined`
21+
- Implicit returns of functions due to missing `return` statements
22+
- `return` statements which don't explicitly return anything
23+
- Lookups of non existent properties
24+
- Function parameters which don't had any explicit value passed
25+
- Anything that has been set to the value of `undefined`
26+
27+
### The case of the "overridden" `undefined`
28+
29+
Since the variable `undefined` only has the value of `undefined`, changing its
30+
value does not change the value of the **type** `undefined`.
31+
32+
Still, in order to compare something against the value of `undefined` it is
33+
necessary to retrieve the value of `undefined` in the first place.
34+
35+
In order to protect code against a possible overwritten `undefined` variable, a
36+
common technique used is to add an additional parameter to the encapsulation
37+
[anonymous wrapper](#scopes), which gets no argument passed to it.
38+
39+
var undefined = 123;
40+
(function(something, foo, undefined) {
41+
// undefined in the local scope does
42+
// now again refer to the value
43+
44+
})('Hello World', 42);
45+
46+
Another way to achieve the same effect would be to use a declaration inside the
47+
wrapper.
48+
49+
var undefined = 123;
50+
(function(something, foo) {
51+
var undefined;
52+
...
53+
54+
})('Hello World', 42);
55+
56+
The only difference being here, that this version results in 4 more bytes being
57+
used in case it is minified and there is no other `var` statement inside the
58+
anonymous wrapper.
59+
60+
### Uses of `null`
61+
62+
While `undefined` in the context of the JavaScript language is mostly used in
63+
the sense of a traditional null, the actual `null` (both a literal and a type)
64+
is more or less just another data type.
65+
66+
It is used in some JavaScript internals (like declaring the end of the
67+
prototype chain by setting `Foo.prototype = null`), but in almost all cases it
68+
can be replaced by `undefined`.
69+

0 commit comments

Comments
 (0)