zoukankan      html  css  js  c++  java
  • [ES6] 15. Generators -- 2

    Using for..of statement:

    function* greeting(){
        console.log(`Generators are "lazy"`);
        yield "How";
        console.log(`I'm not called until the second next`);
        yield "are";
        console.log(`Call me before "you"?`);
        yield "you";
        console.log(`Called when "done"`);
    }
    
    let greeter = greeting();
    for(word of greeter){
        console.log(word);
    }
    
    //How
    //are
    //you

    You can see, it ouptu "How are you", because it grabs the value off of the next.

    The same as:

    let greeter = greeting();
    console.log(greeter.next().value);
    console.log(greeter.next().value);
    console.log(greeter.next().value);
    console.log(greeter.next().value);

    Assign the yield to the variable:

    function* greet(){
        let friendly = yield "How";
        console.log(friendly);
        yield "are";
        yield "you";
    }
    
    var greeter = greet();
    console.log(greeter.next().value);
    console.log(greeter.next().value);
    console.log(greeter.next().value);
    
    //How
    //undefined
    //are 
    //you

    When you assign yield to friendly, it doesn't console out "How" but undefined.

    Why undefined?:

    The way that this works is that the next step through the iteration, so if I say "The heck," will basically send this back through and assign it to this friendly. If I log this out now you'll see I get "How," and then "The heck." That means you can start building things through the iteration process.

    Add param to the next():

    function* greet(){
        let friendly = yield "How";
        console.log(friendly);
        yield "are";
        yield "you";
    }
    
    var greeter = greet();
    console.log(greeter.next().value);
    console.log(greeter.next("the hack").value);
    console.log(greeter.next().value);
    
    //How
    //the hack
    //are 
    //you
    function* greet(){
        let friendly = yield "How";
        friendly = yield friendly + "are";
        yield friendly + "you?";
    }
    
    var greeter = greet();
    console.log(greeter.next().value);
    console.log(greeter.next(" the heck ").value);
    console.log(greeter.next(" silly ol'").value);
    
    //How
    // the heck are
    // silly ol you

    Cannot pass the param to the first next():

    function* greet(){
        let friendly = yield "How";
        friendly = yield friendly + "are";
        yield friendly + "you?";
    }
    
    var greeter = greet();
    console.log(greeter.next("first").value);

    It will show error message:

      TypeError: Sent value to newborn generator.

    Because you haven't given this a chance to run and iterate and go to the next step where you could actually pass in a value.

    总之:yield返回的值是下一个next中所传进来的值

    Generators also help you work with infinite sequences:

    function* graph(){
        let x = 0;
        let y = 0;
        while(true){
            yield {x:x, y:y}
            x += 2;
            y += 1;
        }
    }
    
    
    var graphGenerator = graph();
    console.log(graphGenerator.next().value);
    console.log(graphGenerator.next().value);
    console.log(graphGenerator.next().value);
    console.log(graphGenerator.next().value);
    console.log(graphGenerator.next().value);
    console.log(graphGenerator.next().value);
    console.log(graphGenerator.next().value);
    console.log(graphGenerator.next().value);

    I can safely yield this X and Y point knowing confidently that this stuff isn't going to evaluate until the next step through after the yield process.

  • 相关阅读:
    Monkey Studio IDE | The way IDEs should be
    ImportError: No module named pysqlite2 chinacloud 博客园
    EF架构——code first开发中,在修改实体时,自动影响到数据表上
    你必须要知道的架构知识~目录
    MVC中业务层是否应该有个基类?它有什么作用?
    解决COOKIES存储中文乱码的问题
    C#代码是更具艺术性的,选择她,因为喜欢她
    arm驱动程序——按键程序6_互斥1—原子操作(韦东山的视频总结及针对linux2.6.30)
    Oracle体系结构及备份(十)——sgaothers_pool
    Linux进程间通信(三)管道通信之有名管道及其基础实验
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4117501.html
Copyright © 2011-2022 走看看