Wyrażenie yield*
służy do wydelegowania działania generatora do innego generatora
lub obiektu iterowalnego.
Źródło poniższego interaktywnego przykładu przechowywane jest w repozytorium na GitHub. Jeśli chcesz współtworzyć projekt interaktywnych przykładów, sklonuj https://github.com/mdn/interactive-examples i wyślij nam pull request.
Składnia
yield* [[expression]];
expression
- Wyrażenie, które zwraca iterowalny obiekt lub generator.
Opis
yield*
iteruje po iterowalnym obiekcie i wywołuje yield
z każdą kolejną zwracaną przez niego wartością.
Wartość samego yield*
jest wartością zwróconą przez iterator w momencie jego zakończenia (tzn. kiedy done
ma wartość true
).
Przykłady
Delegowanie logiki do osobnego generatora
W poniższym kodzie wartości yeld
dla g1()
zwracane są przy wywołaniu next()
dokładnie tak samo jak te, które zwraca yeld
generatora g2()
.
function* g1() {
yield 2;
yield 3;
yield 4;
}
function* g2() {
yield 1;
yield* g1();
yield 5;
}
var iterator = g2();
console.log(iterator.next()); // {value: 1, done: false}
console.log(iterator.next()); // {value: 2, done: false}
console.log(iterator.next()); // {value: 3, done: false}
console.log(iterator.next()); // {value: 4, done: false}
console.log(iterator.next()); // {value: 5, done: false}
console.log(iterator.next()); // {value: undefined, done: true}
Inne obiekty iterowalne
yield*
może wywoływać yield
z wartościami dostarczanymi przez inne rodzje obiektów iterowalnych , np. tablice, stringi lub obiekt arguments
.
function* g3() {
yield* [1, 2];
yield* '34';
yield* Array.from(arguments);
}
var iterator = g3(5, 6);
console.log(iterator.next()); // {value: 1, done: false}
console.log(iterator.next()); // {value: 2, done: false}
console.log(iterator.next()); // {value: "3", done: false}
console.log(iterator.next()); // {value: "4", done: false}
console.log(iterator.next()); // {value: 5, done: false}
console.log(iterator.next()); // {value: 6, done: false}
console.log(iterator.next()); // {value: undefined, done: true}
Wartość samego wyrażenia yield*
yield*
jest wyrażeniem (expression) a nie statement, więc rozwiązuje się do konkretnej wartości.
function* g4() {
yield* [1, 2, 3];
return 'foo';
}
var result;
function* g5() {
result = yield* g4();
}
var iterator = g5();
console.log(iterator.next()); // {value: 1, done: false}
console.log(iterator.next()); // {value: 2, done: false}
console.log(iterator.next()); // {value: 3, done: false}
console.log(iterator.next()); // {value: undefined, done: true},
// g4() zwrócił w tym momencie {value: 'foo', done: true}
console.log(result); // "foo"
Specyfikacje
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Yield' in that specification. |
Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of 'Yield' in that specification. |
Draft |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | (Yes) | 27.0 (27.0) | ? | ? | 10 |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | (Yes) | 27.0 (27.0) | ? | ? | 10 |
Firefox-specific notes
- Starting with Gecko 33 (Firefox 33 / Thunderbird 33 / SeaMonkey 2.30), the parsing of the yield expression has been updated to conform with the ES2015 specification (błąd 981599):
- The line terminator restriction is now implemented. No line terminator between "yield" and "*" is allowed. Code like the following will throw a
SyntaxError
:function* foo() { yield *[]; }
- The line terminator restriction is now implemented. No line terminator between "yield" and "*" is allowed. Code like the following will throw a