Skip to main content

Module iterator

Module iterator 

Source
Expand description

Iterator and its helpers (ES2025 §27.1.4 plus the sequencing additions).

Every helper is lazy: xs.map(f) does no work until something calls next. That is the whole point of the API — (function*(){ while (true) yield 1 })().take(3).toArray() has to terminate — so the state a helper needs between calls lives on the returned object and one shared next implementation drives all of them.

The state is ordinary non-enumerable properties rather than a dedicated interior slot because a NativeFn is a plain function pointer and cannot capture. Hidden properties are also what keeps the collector honest: everything a helper holds is a Value in the property table, so a suspended helper keeps its source iterator and callback alive.

Two semantics are easy to get wrong and are called out where they matter:

  • No eager brand check. Iterator.prototype.map.call({}, f) does not throw: the receiver only has to be an object. The failure comes later, when next reads this as an iterator record and finds no callable next.
  • return propagates. Closing a helper closes the iterator underneath it, exactly once, which is what makes using and for…of cleanup visible through a chain.

Functions§

install
Install Iterator.