mozilla
Your Search Results

    function*

    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

    Simple example

    function* idMaker(){
        var index = 0;
        while(true)
            yield index++;
    }
    
    var gen = idMaker();
    
    console.log(gen.next().value); // 0
    console.log(gen.next().value); // 1
    console.log(gen.next().value); // 2
    // ...

    Specification

    • Harmony proposal
    • Draft spec

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
    Basic support 29 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: ziyunfei, Havvy, fscholz, dbruant
    Last updated by: fscholz,