|
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 | 8 | [3]; // Result: [3] |
11 | | - new Array(3); // Result: [] |
12 | | - new Array('3') // Result: ['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`プロパティを伴った新しい*疎*配列を返します。特筆すべきなのは、新しい配列の`length`プロパティ**のみ**が、このようにセットされるという事です。実際の配列のインデックスは初期化されません。 |
19 | 13 |
|
20 | 14 | var arr = new Array(3); |
21 | 15 | arr[1]; // undefined |
22 | | - 1 in arr; // false, the index was not set |
| 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 |
|
|
0 commit comments