function* 演算子は式の中でジェネレーター関数を定義します。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
構文
function* [name]([param1[, param2[, ..., paramN]]]) {
statements
}
引数
name- 関数名。省略可。 省略した場合、関数は無名関数として認識されます。名前は関数本体のみにローカルです。
paramN- 関数に渡される引数の名前。関数は最大 255 の引数を持ち得ます。
statements- 関数の本体を構成するステートメント。
説明
function* 式は function* ステートメントとよく似ており、同じ書式でもあります。function* 式と function* ステートメントの主な違いは、無名ジェネレーター関数を生成するために function* 式で省略された関数名です。詳細はfunctions をご覧ください。
例
次の例では、無名ジェネレーター関数を定義し、x に割り当てます。関数は引数の二乗をもたらします:
var x = function*(y) {
yield y * y;
};
仕様
| 仕様 | ステータス | コメント | |
|---|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) function* の定義 |
標準 | 初期定義。 | |
| ECMAScript Latest Draft (ECMA-262) function* の定義 |
ドラフト |
ブラウザー実装状況
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| デスクトップ | モバイル | サーバー | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
function* | Chrome 完全対応 49 | Edge 完全対応 12 | Firefox 完全対応 26 | IE 未対応 なし | Opera 完全対応 36 | Safari 完全対応 10 | WebView Android 完全対応 49 | Chrome Android 完全対応 49 | Firefox Android 完全対応 26 | Opera Android 完全対応 36 | Safari iOS 完全対応 10 | Samsung Internet Android 完全対応 5.0 | nodejs 完全対応 あり |
| Trailing comma in parameters | Chrome 完全対応 58 | Edge 未対応 なし | Firefox 完全対応 52 | IE 未対応 なし | Opera 完全対応 45 | Safari ? | WebView Android 完全対応 58 | Chrome Android 完全対応 58 | Firefox Android 完全対応 52 | Opera Android 完全対応 43 | Safari iOS ? | Samsung Internet Android 完全対応 7.0 | nodejs 完全対応 8.0.0 |
凡例
- 完全対応
- 完全対応
- 未対応
- 未対応
- 実装状況不明
- 実装状況不明