账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    请教扩展运算符和for...of在遍历generator函数时的输出顺序为什么不一致?
    41
    0

    先上代码:

    function* numbers () {
      yield 1
      yield 2
      console.log('hint')
      return 3
      yield 4
    }
    
    console.log([...numbers()]) 
    // 输出 
    // hint
    // [ 1, 2 ]
    
    for (let n of numbers()) {
      console.log(n)
    }
    // 输出
    // 1
    // 2
    // hint
    

    这是我在学习阮老师的es6的“Generator 函数的语法”这一章时发现扩展运算符...在遍历有输出或有return返回时的generator函数时的输出顺序跟我的预想不一样,而for...of则比较符合直觉。
    按阮老师的说法“for...of循环、扩展运算符(...)、解构赋值和Array.from方法内部调用的都是遍历器接口”,那输出顺序应该是一致的呀?
    请有心人帮忙解释。

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部