From 4f1cf93bd612c68ce2a21a9ece6a6715b5dbc308 Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Wed, 16 Feb 2022 12:22:06 +0900 Subject: Web/JavaScript/Reference/Operators 以下の残りの記事を移行 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reference/operators/yield/index.html | 119 --------------------- 1 file changed, 119 deletions(-) delete mode 100644 files/ja/web/javascript/reference/operators/yield/index.html (limited to 'files/ja/web/javascript/reference/operators/yield/index.html') diff --git a/files/ja/web/javascript/reference/operators/yield/index.html b/files/ja/web/javascript/reference/operators/yield/index.html deleted file mode 100644 index 2b2c68b691..0000000000 --- a/files/ja/web/javascript/reference/operators/yield/index.html +++ /dev/null @@ -1,119 +0,0 @@ ---- -title: yield -slug: Web/JavaScript/Reference/Operators/yield -tags: - - ECMAScript 2015 - - Generators - - Iterator - - JavaScript - - Language feature - - Operator -browser-compat: javascript.operators.yield -translation_of: Web/JavaScript/Reference/Operators/yield ---- -
{{jsSidebar("Operators")}}
- -

yield キーワードは、ジェネレーター関数 ({{jsxref("Statements/function*", "function*")}} または古いジェネレーター関数) を一時停止したり再開したりするために使用します。

- -
{{EmbedInteractiveExample("pages/js/expressions-yield.html", "taller")}}
- -

構文

- -
[rv] = yield [expression]
- -
-
expression {{optional_inline}}
-
iterator プロトコル経由でジェネレーター関数が返す値を定義します。省略した場合、代わりに undefined が返されます。
-
rv {{optional_inline}}
-
-

ジェネレーターの実行を再開する next() メソッドに渡したオプションの値を受け取ります。

-
-
- -

解説

- -

yield キーワードは、ジェネレーター関数の実行を一時停止し、ジェネレーターの呼び出し元に yield キーワードに続く値を返します。これは、 return キーワードのジェネレーター版と考えることができます。

- -

yield はそれを含むジェネレーター関数の中で直接しか呼び出すことしかできません。呼び出し先の関数やコールバックから呼び出すことはできません。

- -

yield キーワードはジェネレーターの next() メソッドを呼び出させ、 valuedone の 2 つのプロパティを持つ IteratorResult オブジェクトを返します。 value プロパティは yield 式の評価結果であり、 donefalse、すなわちジェネレーター関数が完全には完了していないことを示します。

- -

yield 式によって実行が停止されると、ジェネレーターの next() メソッドが呼び出されるまで、ジェネレーターのコード実行は一時停止します。ジェネレーターの next() メソッドが呼ばれるたびに、ジェネレーターの実行が再開され、次のうちのいずれかに達するまで実行されます。

- - - -

ジェネレーターの next() メソッドにオプションの値が渡された場合、その値はジェネレーターの現在の yield 操作の返値となります。

- -

ジェネレーターのコードパス、 yield 演算子、新しい開始値を {{jsxref("Generator.prototype.next()")}} に渡すことで指定することができる機能により、ジェネレーターは大きな力と制御を提供します。

- -
-

警告: 残念ながら next() は非対称ですが、仕方がありません。常に現在中断している yield に値を送りますが、次の yield のオペランドを返します。

-
- -

- -

Using yield

- -

次のコードはジェネレーター関数の定義例です。

- -
function* countAppleSales () {
-  let saleList = [3, 7, 5]
-  for (let i = 0; i < saleList.length; i++) {
-    yield saleList[i]
-  }
-}
- -

ジェネレーター関数が定義されると、ご覧のようにイテレーターを構築するために使用されます。

- -
let appleStore = countAppleSales()  // Generator { }
-console.log(appleStore.next())      // { value: 3, done: false }
-console.log(appleStore.next())      // { value: 7, done: false }
-console.log(appleStore.next())      // { value: 5, done: false }
-console.log(appleStore.next())      // { value: undefined, done: true }
- -

You can also send a value with next(value) into the generator. 'step' evaluates as a - return value in this syntax [rv] = yield - [expression]

- -
function* counter(value) {
- let step;
-
- while (true) {
-   step = yield ++value;
-
-   if (step) {
-     value += step;
-   }
- }
-}
-
-const generatorFunc = counter(0);
-console.log(generatorFunc.next().value);   // 1
-console.log(generatorFunc.next().value);   // 2
-console.log(generatorFunc.next().value);   // 3
-console.log(generatorFunc.next(10).value); // 14
-console.log(generatorFunc.next().value);   // 15
-console.log(generatorFunc.next(10).value); // 26
- -

仕様書

- -{{Specifications}} - -

ブラウザーの互換性

- -

{{Compat}}

- -

関連情報

- - -- cgit v1.2.3-54-g00ecf