Generator的基本用法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Generator基本用法</title>
</head>
<body>
<script type="text/javascript">
    //每个遍历器之间互不干扰,作用域独立
    function* g() {
        var o = 1;
        yield o++;
        yield o++;
        yield o++;
    }
    var gen = g();
    console.log(gen.next().value); // 1
    var xxx = g();
    console.log(gen.next().value); // 2
    console.log(xxx.next().value); // 1
    console.log(gen.next().value); // 3

    //next方法参数的作用,是覆盖掉上一个yield语句的值
    function* g2()
    {
        var o = 1;
        var a = yield o++;
        console.log('a = ' + a);
        var b = yield o++;
    }
    var gen = g2();
    console.log(gen.next().value);
    console.log('------');
    console.log(gen.next(11));

    //for...of循环可以自动遍历Generator函数时生成的Iterator对象,且此时不再需要调用next方法
    //下面代码使用for...of循环,依次显示5个yield语句的值。这里需要注意,一旦next方法的返回对
    // 象的done属性为true,for...of循环就会中止,且不包含该返回对象,所以上面代码的return语句
    // 返回的6,不包括在for...of循环之中
    function* foo() {
        yield 1;
        yield 2;
        yield 3;
        yield 4;
        yield 5;
        return 6;
    }

    let a = foo();

    for (let v of a) {
        console.log(v);
    }
    // 1 2 3 4 5

    //Generator函数返回的遍历器对象,还有一个return方法,可以返回给定的值,并且终结遍历Generator函数
    //就是说,return的参数值覆盖本次yield语句的返回值,并且提前终结遍历,即使后面还有yield语句也一律无视
    function* gen2() {
        yield 1;
        yield 2;
        yield 3;
    }
    var g = gen2();
    console.log(g.next());          // { value: 1, done: false }
    console.log(g.return('foo'));   // { value: "foo", done: true }
    console.log(g.next());          // {value: undefined, done: true}

    //在Generater函数内部,调用另一个Generator函数,用yield*语句
    function* foo2() {
        yield 'a';
        yield 'b';
    }

    function* bar() {
        yield 'x';
        yield* foo2();
        yield 'y';
    }

    for (let v of bar()){
        console.log(v);
    }
    // "x"
    // "a"
    // "b"
    // "y"

</script>
</body>
</html>




转载于:https://www.cnblogs.com/xutongbao/p/9924963.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值