|
1 | | -## The `for in` Loop |
| 1 | +## `for in`ループ |
2 | 2 |
|
3 | | -Just like the `in` operator, the `for in` loop also traverses the prototype |
4 | | -chain when iterating over the properties of an object. |
| 3 | +`in`オペレーターは単に、`for in`ループの中でオブジェクトのプロパティをプロトタイプチェーンの中で繰り返し遡る為にあるものです。 |
5 | 4 |
|
6 | | -> **Note:** The `for in` loop will **not** iterate over any properties that |
7 | | -> have their `enumerable` attribute set to `false`; for example, the `length` |
8 | | -> property of an array. |
9 | | - |
10 | | - // Poisoning Object.prototype |
| 5 | +> **注意:** `for in`ループは`列挙`される属性が`false`にセットされているプロパティを反復処理**しません**。; |
| 6 | +> 例えば、配列の`length`プロパティなどがそれに当たります。 |
| 7 | +
|
| 8 | + // Object.prototype汚染 |
11 | 9 | Object.prototype.bar = 1; |
12 | 10 |
|
13 | 11 | var foo = {moo: 2}; |
14 | 12 | for(var i in foo) { |
15 | | - console.log(i); // prints both bar and moo |
| 13 | + console.log(i); // barとmooが両方とも表示される |
16 | 14 | } |
17 | 15 |
|
18 | | -Since it is not possible to change the behavior of the `for in` loop itself, it |
19 | | -is necessary to filter out the unwanted properties inside the loop body , |
20 | | -this is done by using the [`hasOwnProperty`](#object.hasownproperty) method of |
21 | | -`Object.prototype`. |
| 16 | +`for in`ループそれ自体の動作を変更する事は不可能ですが、ループ内にある要らないプロパティをフィルタリングする必要があります。そんな時は`Object.prototype`の[`hasOwnProperty`](#object.hasownproperty)メソッドを使うと解決します。 |
22 | 17 |
|
23 | | -> **Note:** Since the `for in` always traverses the complete prototype chain, it |
24 | | -> will get slower with each additional layer of inheritance added to an object. |
| 18 | +> **注意:** `for in`は常にプロトタイプチェーンを完全に遡ります。これにより |
| 19 | +> オブジェクトに追加されている継承が多ければ多い程、速度は遅くなります。 |
25 | 20 |
|
26 | | -### Using `hasOwnProperty` for Filtering |
| 21 | +### `hasOwnProperty` for Filtering |
27 | 22 |
|
28 | 23 | // still the foo from above |
29 | 24 | for(var i in foo) { |
|
0 commit comments