The Generator
object is returned by a generator function and it conforms to both the iterable protocol and the iterator protocol.
This object cannot be instantiated directly. Instead, a Generator
instance can be returned from a generator function:
function* generator() { yield 1; yield 2; yield 3; } const gen = generator(); // "Generator { }"
Generator.prototype.next()
yield
expression.Generator.prototype.return()
Generator.prototype.throw()
function* infinite() { let index = 0; while (true) { yield index++; } } const generator = infinite(); // "Generator { }" console.log(generator.next().value); // 0 console.log(generator.next().value); // 1 console.log(generator.next().value); // 2 // ...
Desktop | ||||||
---|---|---|---|---|---|---|
Generator |
39 | 13 | 26 | No | 26 | 10 |
next |
39 | 13 | 26 | No | 26 | 10 |
return |
50 | 13 | 38 | No | 37 | 10 |
throw |
39 | 13 | 26 | No | 26 | 10 |
Mobile | ||||||
---|---|---|---|---|---|---|
Generator |
39 | 39 | 26 | 26 | 10 | 4.0 |
next |
39 | 39 | 26 | 26 | 10 | 4.0 |
return |
50 | 50 | 38 | 37 | 10 | 5.0 |
throw |
39 | 39 | 26 | 26 | 10 | 4.0 |
Server | |
---|---|
Generator |
4.0.0
|
next |
Yes |
return |
6.0.0 |
throw |
4.0.0
|
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator