Skip to content

Commit b50f2c3

Browse files
author
hminaya
committed
Ok, buen inicio
1 parent 82fd3b6 commit b50f2c3

28 files changed

+3777
-1
lines changed

doc/es.zip

31.7 KB
Binary file not shown.

doc/es/array/constructor.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## The `Array` Constructor
2+
3+
Since the `Array` constructor is ambiguous in how it deals with its parameters,
4+
it is highly recommended to always use the array literals - `[]` notation -
5+
when creating new arrays.
6+
7+
[1, 2, 3]; // Result: [1, 2, 3]
8+
new Array(1, 2, 3); // Result: [1, 2, 3]
9+
10+
[3]; // Result: [3]
11+
new Array(3); // Result: []
12+
new Array('3') // Result: ['3']
13+
14+
In cases when there is only one argument passed to the `Array` constructor,
15+
and that argument is a `Number`, the constructor will return a new *sparse*
16+
array with the `length` property set to the value of the argument. It should be
17+
noted that **only** the `length` property of the new array will be set this way,
18+
the actual indexes of the array will not be initialized.
19+
20+
var arr = new Array(3);
21+
arr[1]; // undefined
22+
1 in arr; // false, the index was not set
23+
24+
The behavior of being able to set the length of the array upfront only comes in
25+
handy in a few cases, like repeating a string, in which it avoids the use of a
26+
`for loop` code.
27+
28+
new Array(count + 1).join(stringToRepeat);
29+
30+
### In Conclusion
31+
32+
The use of the `Array` constructor should be avoided as much as possible.
33+
Literals are definitely preferred. They are shorter and have a clearer syntax;
34+
therefore, they also increase the readability of the code.
35+

doc/es/array/general.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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
5+
are 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 the properties that are on the prototype
12+
chain and 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, that 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.
58+

doc/es/core/eval.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Why Not to Use `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 called function is actually `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+
`eval` also is a security problem as it executes **any** code given to it,
39+
it should **never** be used with strings of unknown or untrusted origins.
40+
41+
### In Conclusion
42+
43+
`eval` should never be used, any code that makes use of it is to be questioned in
44+
its workings, performance and security. In case something requires `eval` in
45+
order to work, its design is to be questioned 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/es/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 parser 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,
105+
the above will yield a `TypeError` stating 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/es/core/undefined.md

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

0 commit comments

Comments
 (0)