Links
- Expressions and operators | MDN
- function*
- yield
- yield*
- [for (x of y) x]
- Generator comprehensions (ES7)
(for (x of iterable) x)
(for (x of iterable) if (condition) x)
(for (x of iterable) for (y of iterable) x + y)
- List Comprehensions (ES7)
[for (x of iterable) x]
[for (x of iterable) if (condition) x]
[for (x of iterable) for (y of iterable) x + y]
- for...of Statement
- super
- ...obj
f(-1, ...args, 2, ...[3])
['head', ...parts, 'and', 'toes']
[a, b, ...iterableObj] = [1, 2, 3, 4, 5]
Snippets
Counter
function* count(initial) {
var i = initial;
while(true) {
yield i++;
}
}
var c = count(0);
console.log(c.next().value); // 0
console.log(c.next().value); // 1
console.log(c.next().value); // 2
Chaining Generators
Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*#Example_with_yield*
// yield* to yield from another generator
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(gen.next().value); // 10
console.log(gen.next().value); // 11
console.log(gen.next().value); // 12
console.log(gen.next().value); // 13
console.log(gen.next().value); // 20