Skip to content

Commit 04142c1

Browse files
committed
Translate array constructor
1 parent 4a93411 commit 04142c1

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

doc/pt/constructor.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## O constructor do `Array`
2+
3+
Visto que o constructor do `Array` é ambíguo na forma como age com os seus parâmetros,
4+
é altamente recomendado utilizar a notação literal do Array - `[]` -
5+
na criação de novos vectores.
6+
7+
[1, 2, 3]; // Resultado: [1, 2, 3]
8+
new Array(1, 2, 3); // Resultado: [1, 2, 3]
9+
10+
[3]; // Resultado: [3]
11+
new Array(3); // Resultado: []
12+
new Array('3') // Resultado: ['3']
13+
14+
No caso de haver só um parâmetro passado ao constructor do `Array`, e
15+
esse argumento ser um `Number`, o constructor retorna um novo
16+
vector *esparço* com a propriedade `length` com o valor do argumento.
17+
Além disso, ** a propriedade `length` do novo vector é definida desta
18+
maneira; os indices do vector não são inicializados.
19+
20+
var arr = new Array(3);
21+
arr[1]; // indefinido
22+
1 in arr; // falso, o índice nao foi definido
23+
24+
O facto de ser possivel definir o tamanho do vector inicialmente só
25+
é util nalguns casos, tal como na repetição de um objecto de tipo
26+
`String´, visto que evita a utilização de um ciclo `for`.
27+
28+
new Array(count + 1).join(stringToRepeat);
29+
30+
### Em conclusão
31+
32+
O uso do constructor do `Array` deve ser evitado tanto como possivel. Os
33+
literais são definitivamente preferidos. Sao mais curtos e tem uma
34+
sintaxe mais clara; o que aumenta a legibilidade do código.

doc/pt/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+
Because the `for in` loop enumerates all the properties that are on the prototype
12+
chain and because 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.
58+

0 commit comments

Comments
 (0)