aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/errors/is_not_iterable
diff options
context:
space:
mode:
authorMasahiro FUJIMOTO <mfujimot@gmail.com>2021-06-12 03:10:39 +0900
committerpotappo <potappo@gmail.com>2021-06-21 23:09:03 +0900
commitc43ef332c9395963ad974bcf8850bdac79f7c55c (patch)
treeb0ac906bcefd76328250f3da09847286ccd16144 /files/ja/web/javascript/reference/errors/is_not_iterable
parentee4967e7aea9cdb4b9298f9e4b3bd9e80a48cf3f (diff)
downloadtranslated-content-c43ef332c9395963ad974bcf8850bdac79f7c55c.tar.gz
translated-content-c43ef332c9395963ad974bcf8850bdac79f7c55c.tar.bz2
translated-content-c43ef332c9395963ad974bcf8850bdac79f7c55c.zip
Web/JavaScript/Reference/Errors/I-J* を更新
2021/06/11 時点の英語版に同期
Diffstat (limited to 'files/ja/web/javascript/reference/errors/is_not_iterable')
-rw-r--r--files/ja/web/javascript/reference/errors/is_not_iterable/index.html58
1 files changed, 42 insertions, 16 deletions
diff --git a/files/ja/web/javascript/reference/errors/is_not_iterable/index.html b/files/ja/web/javascript/reference/errors/is_not_iterable/index.html
index 6da6ca9b19..665371733f 100644
--- a/files/ja/web/javascript/reference/errors/is_not_iterable/index.html
+++ b/files/ja/web/javascript/reference/errors/is_not_iterable/index.html
@@ -10,9 +10,11 @@ translation_of: Web/JavaScript/Reference/Errors/is_not_iterable
---
<div>{{jsSidebar("Errors")}}</div>
-<h2 id="メッセージ">メッセージ</h2>
+<p>JavaScript の例外 "is not iterable" は、 <a href="/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of</a> の右辺として与えられた値や、 {{jsxref("Promise.all")}} または {{jsxref("TypedArray.from")}} のような関数の引数として与えられた値が<a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols">反復可能オブジェクト</a>ではなかった場合に発生します。</p>
-<pre class="syntaxbox">TypeError: 'x' is not iterable (Firefox, Chrome)
+<h2 id="Message">エラーメッセージ</h2>
+
+<pre class="brush: js">TypeError: 'x' is not iterable (Firefox, Chrome)
TypeError: 'x' is not a function or its return value is not iterable (Chrome)
</pre>
@@ -22,13 +24,13 @@ TypeError: 'x' is not a function or its return value is not iterable (Chrome)
<h2 id="何がうまくいかなかったのか?">何がうまくいかなかったのか?</h2>
-<p><a href="/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of</a> の右側や {{jsxref("Promise.all")}} や {{jsxref("TypedArray.from")}} のような関数の引数として与えられた値が <a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols">反復可能オブジェクト</a> ではありません。反復可能なものは、{{jsxref("Array")}} や {{jsxref("String")}}、{{jsxref("Map")}}、ジェネレーターの結果のようなビルトイン反復可能型や <a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol">反復処理プロトコル</a> を実装したオブジェクトです。</p>
+<p><a href="/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of</a> の右辺、 {{jsxref("Promise.all")}} や {{jsxref("TypedArray.from")}} などの引数として指定された値が<a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols">反復可能オブジェクト</a>ではありません。反復可能なものは、 {{jsxref("Array")}}, {{jsxref("String")}}, {{jsxref("Map")}} 等のような組み込み反復可能型や、ジェネレーターの結果、<a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol">反復可能プロトコル</a>を実装しているオブジェクトが成ることができます。</p>
<h2 id="例">例</h2>
-<h3 id="オブジェクトのプロパティを反復処理する">オブジェクトのプロパティを反復処理する</h3>
+<h3 id="Iterating_over_Object_properties">オブジェクトのプロパティの反復処理</h3>
-<p>JavaScript では、<a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol">反復処理プロトコル</a> を実装していない限り {{jsxref("Object")}} は反復処理できません。それゆえ、オブジェクトのプロパティを反復処理するために <a href="/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of</a> を使用することはできません。</p>
+<p>JavaScript では、 {{jsxref("Object")}} は<a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol">反復処理プロトコル</a> を実装していない限り反復処理できません。したがって、オブジェクトのプロパティを反復処理するために <a href="/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of</a> を使用することはできません。</p>
<pre class="brush: js example-bad">var obj = { 'France': 'Paris', 'England': 'London' };
for (let p of obj) { // TypeError: obj is not iterable
@@ -48,10 +50,9 @@ for (let country of Object.keys(obj)) {
for (const [country, capital] of Object.entries(obj))
console.log(country, capital);
-
</pre>
-<p>このユースケースのそのほかの選択肢として、{{jsxref("Map")}} を使用することもできます:</p>
+<p>この使用例のそのほかの選択肢として、{{jsxref("Map")}} を使用することもできます。</p>
<pre class="brush: js example-good">var map = new Map;
map.set('France', 'Paris');
@@ -69,9 +70,9 @@ for (const [country, capital] of map.entries())
console.log(country, capital);
</pre>
-<h3 id="ジェネレーターを反復処理する">ジェネレーターを反復処理する</h3>
+<h3 id="Iterating_over_a_generator">ジェネレーターを反復処理する</h3>
-<p><a href="/ja/docs/Web/JavaScript/Guide/Iterators_and_Generators#Generators">ジェネレーター</a> は反復可能オブジェクトを生成するために呼び出す関数です。</p>
+<p><a href="/ja/docs/Web/JavaScript/Guide/Iterators_and_Generators#generators">ジェネレーター</a> 反復可能オブジェクトを生成するために呼び出す関数です。</p>
<pre class="brush: js example-bad">function* generate(a, b) {
yield a;
@@ -93,13 +94,38 @@ for (let x of generate(1,2))
console.log(x);
</pre>
-<h2 id="関連項目">関連項目</h2>
+<h3 id="Iterating_over_a_custom_iterable">独自の反復可能オブジェクトでの反復処理</h3>
+
+<p>独自の反復可能オブジェクトは、 {{jsxref("Symbol.iterator")}} メソッドを実装することで作成することができます。 iterator メソッドはイテレーターであるオブジェクト、すなわち next メソッドを持っている必要があります。
+</p>
+
+<pre class="brush: js example-bad">const myEmptyIterable = {
+ [Symbol.iterator]() {
+ return [] // [] は反復可能ですが、イテレーターではありません。 -- next メソッドがないからです。
+ }
+}
+
+Array.from(myEmptyIterable); // TypeError: myEmptyIterable is not iterable
+</pre>
+
+<p>こちらは正しい実装です。</p>
+
+<pre class="brush: js example-good">const myEmptyIterable = {
+ [Symbol.iterator]() {
+ return [][Symbol.iterator]()
+ }
+}
+
+Array.from(myEmptyIterable); // []
+</pre>
+
+<h2 id="See_also">関連情報</h2>
<ul>
- <li><a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol">反復処理プロトコル</a></li>
- <li>{{jsxref("Object.keys")}}</li>
- <li>{{jsxref("Object.entries")}}</li>
- <li>{{jsxref("Map")}}</li>
- <li><a href="/ja/docs/Web/JavaScript/Guide/Iterators_and_Generators#Generators">イテレーターとジェネレーター</a></li>
- <li><a href="/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of</a></li>
+ <li><a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol">反復処理プロトコル</a></li>
+ <li>{{jsxref("Object.keys")}}</li>
+ <li>{{jsxref("Object.entries")}}</li>
+ <li>{{jsxref("Map")}}</li>
+ <li><a href="/ja/docs/Web/JavaScript/Guide/Iterators_and_Generators#generators">ジェネレーター</a></li>
+ <li><a href="/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of</a></li>
</ul>