Skip to main content

function* in JavaScript


In this page I found a new JavaScript function type:




// NOTE: "function*" is not supported yet in Firefox.
// Remove the asterisk in order for this code to work in Firefox 13

function* fibonacci() { // !!! this is the interesting line !!!
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}



I already know what yeild , let and [?,?]=[?,?] do , but have no idea what the function* is meant to be. What is it?



P.S. don't bother trying Google, it's impossible to search for expressions with asterisks ( they're used as placeholders ).



Thanks!


Source: Tips4allCCNA FINAL EXAM

Comments

  1. It's a proposed syntax for EcmaScript.next.

    Dave Herman of Mozilla gave a talk about EcmaScript.next. At 30:15 he talks about generators. Earlier, he explains how Mozilla is experimentally implementing proposed language changes to help steer the committee. Dave works closely with Brendan Eich, Mozilla's CTO (I think), and the original JavaScript designer.

    You can find more detail on the EcmaScript working group wiki: http://wiki.ecmascript.org/doku.php?id=harmony:generators

    The working group (TC-39) has general agreement that EcmaScript.next should have some kind of generator iterator proposal, but this is not final. You shouldn't rely on this showing up without changes in the next version of the language, and even if it doesn't change, it probably won't show up widely in other browsers for a while.


    Overview

    First-class coroutines, represented as objects encapsulating suspended execution contexts (i.e., function activations). Prior art: Python, Icon, Lua, Scheme, Smalltalk.

    Examples

    The “infinite” sequence of Fibonacci numbers (notwithstanding behavior around 253):

    function* fibonacci() {
    let [prev, curr] = [0, 1];
    for (;;) {
    [prev, curr] = [curr, prev + curr];
    yield curr;
    }
    }


    Generators can be iterated over in loops:

    for (n of fibonacci()) {
    // truncate the sequence at 1000
    if (n > 1000)
    break;
    print(n);
    }


    Generators are iterators:

    let seq = fibonacci();
    print(seq.next()); // 1
    print(seq.next()); // 2
    print(seq.next()); // 3
    print(seq.next()); // 5
    print(seq.next()); // 8

    ReplyDelete
  2. The function* type looks like it acts as a generator function for processes that can be iterated.
    C# has a feature like this using "yield return" see 1 and see 2

    Essentially this returns each value one by one to whatever is iterating this function, which is why their use case shows it in a foreach style loop.

    ReplyDelete
  3. It's a generator function - and it said so in the page you cite, in the comment you replaced with "this is the interesting line"...

    Basically it's a way to specify sequences programmatically so that they can be passed around and elements accessed by index without having to compute the entire sequence (possibly infinite in size) beforehand.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex