|
1 |
| -## The `Array` Constructor |
| 1 | +## Конструктор `Array` |
2 | 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. |
| 3 | +Так как в конструкторе `Array` есть некоторая двусмысленность кастательно его параметров, настоятельно рекомендуется всегда использовать синтаксис литеральной нотации — `[]` — при создании массивов. |
6 | 4 |
|
7 |
| - [1, 2, 3]; // Result: [1, 2, 3] |
8 |
| - new Array(1, 2, 3); // Result: [1, 2, 3] |
| 5 | + [1, 2, 3]; // Результат: [1, 2, 3] |
| 6 | + new Array(1, 2, 3); // Результат: [1, 2, 3] |
9 | 7 |
|
10 |
| - [3]; // Result: [3] |
11 |
| - new Array(3); // Result: [] |
12 |
| - new Array('3') // Result: ['3'] |
| 8 | + [3]; // Результат: [3] |
| 9 | + new Array(3); // Результат: [] |
| 10 | + new Array('3') // Результат: ['3'] |
13 | 11 |
|
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. |
| 12 | +В случае, когда в конструктор `Array` передается один аргумент и этот аргумент имеет тип `Number`, конструктор возвращает новый, *запоненный случайными значениями*, массив, имеющий длину равную значению переданного аргумента. Стоит заметить, что в этом случае будет установлено только свойство `length` нового массива, фактически индексы массива не будут проинициализированы. |
19 | 13 |
|
20 | 14 | var arr = new Array(3);
|
21 |
| - arr[1]; // undefined |
22 |
| - 1 in arr; // false, the index was not set |
| 15 | + arr[1]; // не определён, undefined |
| 16 | + 1 in arr; // false, индекс не был установлен |
23 | 17 |
|
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. |
| 18 | +Поведение, которое позволяет изначально установить только размер массива может пригодиться лишь в нескольких случаях, таких как повторение строк, за счёт чего избегается использование цикла `for loop`. |
27 | 19 |
|
28 | 20 | new Array(count + 1).join(stringToRepeat);
|
29 | 21 |
|
30 |
| -### In conclusion |
| 22 | +### Заключение |
31 | 23 |
|
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. |
| 24 | +Использование конструктора `Array` нужно избегать, насколько это возможно. Литералы определённо предпочтительнее — это краткая запись и она имеет более понятный синтаксис, так что при этом даже улучшается читабельность кода. |
35 | 25 |
|
0 commit comments