Implement the iterator protocol, generator functions, and destructuring syntax.
Scope#
Iterator Protocol#
Symbol.iteratormethod on iterables (Array, String, Map, Set, etc.)- Iterator objects:
{ next() → { value, done } } for...ofloop consuming iterators- Spread operator (
...) in array literals and function calls Array.from()consuming iterables
Generator Functions#
function*declaration and expression syntaxyieldexpression — pause execution and produce value- Generator objects conform to both iterator and iterable protocols
generator.next(value)— resume with valuegenerator.return(value)— force completiongenerator.throw(error)— throw into generatoryield*delegation to sub-iterators
Destructuring#
- Array destructuring:
let [a, b, ...rest] = arr - Object destructuring:
let { x, y: alias, ...rest } = obj - Default values:
let [a = 1] = [],let { x = 2 } = {} - Nested destructuring:
let { a: { b } } = obj - Destructuring in function parameters
- Destructuring in for...of:
for (let [key, val] of map) - Computed property names in destructuring:
let { [expr]: val } = obj
Acceptance Criteria#
- for...of works with arrays, strings, Maps, Sets
- Custom iterables work (objects with Symbol.iterator)
- Spread operator works in arrays and function calls
- Generator functions pause and resume correctly
- yield* delegates to sub-iterators
- generator.next(value) passes value into generator
- generator.return and generator.throw work
- Array destructuring with rest and defaults
- Object destructuring with aliases, rest, and defaults
- Nested destructuring works
- Destructuring in function parameters
- Unit tests for iterators, generators, and destructuring
Phase 10 — JavaScript Engine (issue 14 of 15). Depends on: JS Functions/closures, Symbol built-in.