翻譯不完整。請協助 翻譯此英文文件

 function*  宣告式 (保留字 function 後面跟著一個星號) 定義了一個 generator function, 會回傳一個 Generator 物件。

你可以透過 GeneratorFunction 以及 function* expression 來定義一個 generator functions。


function* name([param[, param[, ... param]]]) {
function 名稱。
要被傳入 function 的參數名稱,一個 function 最多擁有 255 個參數。
statements 包含了 function 的內容。


Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.

呼叫 Generator function 不會立即執行裡面的程,而是會回傳一個 iterator 物件。當呼叫 iterator 的 next() 方法時,Generator function 將會執行直到碰到 yield 表達式,它會將值從 iterator 中回傳,如果是 yield* 則會連接到另一個 Generator function。next() 方法會回傳一個 Generator 產生出數值的 value 屬性的物件,此物件還擁有一個 done 屬性,如果該 iterator 物件為最新產生出的,done屬性為 true ,反之則為 false。Calling the next() method with an argument will resume the generator function execution, replacing the yield statement where execution was paused with the argument fromnext()



function* idMaker(){
  var index = 0;
  while(index < 3)
    yield index++;

var gen = idMaker();

console.log(; // 0
console.log(; // 1
console.log(; // 2
console.log(; // undefined
// ...

yield* 的範例

function* anotherGenerator(i) {
  yield i + 1;
  yield i + 2;
  yield i + 3;

function* generator(i){
  yield i;
  yield* anotherGenerator(i);
  yield i + 10;

var gen = generator(10);

console.log(; // 10
console.log(; // 11
console.log(; // 12
console.log(; // 13
console.log(; // 20

Generators 無法被建構

function* f() {}
var obj = new f; // throws "TypeError: f is not a constructor"


Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'function*' in that specification.
Standard Initial definition.
ECMAScript 2016 (ECMA-262)
The definition of 'function*' in that specification.
Standard Changed that generators should not have [[Construct]] trap and will throw when used with new.
ECMAScript 2017 Draft (ECMA-262)
The definition of 'function*' in that specification.


Feature Chrome Firefox (Gecko) Internet Explorer  Edge Opera Safari (WebKit)
Basic support 39.0 26.0 (26.0) No support 13 26 No support
yield* (Yes) 27.0 (27.0) No support 13 26 No support
IteratorResult object instead of throwing (Yes) 29.0 (29.0) No support 13 (Yes) No support
Not constructable with new as per ES2016 ? 43.0 (43.0) ? ? ? ?
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support No support (Yes) 26.0 (26.0) No support No support No support 39.0
yield* No support (Yes) 27.0 (27.0) No support No support No support (Yes)
IteratorResult object instead of throwing No support ? 29.0 (29.0) No support No support No support (Yes)
Not constructable with new as per ES2016 ? ? 43.0 (43.0) ? ? ? ?

Firefox 規範筆記

Generators and iterators in Firefox versions before 26

Older Firefox versions implement an older version of the generators proposal. In the older version, generators were defined using a regular function keyword (without an asterisk) among other differences. See Legacy generator function for further information.

IteratorResult object returned instead of throwing

Starting with Gecko 29 (Firefox 29 / Thunderbird 29 / SeaMonkey 2.26), the completed generator function no longer throws a TypeError "generator has already finished". Instead, it returns an IteratorResult object like { value: undefined, done: true } (bug 958951).



 此頁面的貢獻者: ALiangLiang, tom76kimo
 最近更新: ALiangLiang,