Generators

This is an experimental technology, part of the Harmony (EcmaScript 6) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

Summary

Generators functions enable writing iterators more easily.

Expression
Implemented in: Firefox 26 (SpiderMonkey 26)
ECMA Version: ECMAScript 6th Edition (Draft)

Syntax

function* gen(i){
  while(true){
    yield i++;  
  }
}

 

Description

Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.

 

Examples


Specification

  • Harmony proposal
  • Draft spec

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support yes (when?) 26.0 (26.0) Not supported Not supported Not supported
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support yes (when?) 27.0 (27.0) Not supported Not supported Not supported

 

See also

Document Tags and Contributors

Contributors to this page: dbruant
Last updated by: dbruant,