generator

作者: asmuzi | 来源:发表于2018-12-21 15:41 被阅读0次
    generator
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
            function* show() {
                console.log('a');
    
                yield;
    
                console.log('b');
            }
    
            let obj = show();
    
            obj.next();
            obj.next();
        </script>
    </head>
    
    <body>
    
    </body>
    
    </html>
    
    generator2
    tip:yield可以与返回值
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
            function* show() {
                console.log('a');
    
                yield 12;
    
                console.log('b');
    
                return 5;
            }
    
            let obj = show();
    
            let res1 = obj.next();  //{value:12,done:false}
            console.log(res1);
    
            let res2 = obj.next(); //{value:5,done:true}
            console.log(res2);
        </script>
    </head>
    
    <body>
    
    </body>
    
    </html>
    
    generator2
    tip:yield可以传参
    
    generator3
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title></title>
        <script>
        function *show(a,b){
          alert('a');
          console.log(a,b);
    
          let c=yield;
          console.log(c);
    
          alert('b');
    
          return 5;
        }
    
        let obj=show(12, 5);
    
        let res1=obj.next();
        let res2=obj.next(888);
        </script>
      </head>
      <body>
    
      </body>
    </html>
    
    generator4
    <!DOCTYPE html>
    <html>
    
    <head>
      <meta charset="utf-8">
      <title></title>
      <script src="runner.js" charset="utf-8"></script>
      <script src="jquery.js" charset="utf-8"></script>
      <script>
        //
        runner(function* () {
          alert('欢迎哈');
    
          let arr = yield $.ajax({ url: 'data/1.txt', dataType: 'json' });
    
          alert('接收到了数组' + arr);
    
          let json = yield $.ajax({ url: 'data/2.txt', dataType: 'json' });
    
          alert('json也读完了');
    
          console.log(json);
        });
    
        //
        alert('欢迎哈');
    
        $.ajax({
          url: 'data/1.txt',
          dataType: 'json',
          success(arr) {
            console.log('接收到了数组' + arr);
            $.ajax({
              url: 'data/2.txt',
              dataType: 'json',
              success(json) {
                console.log('json也读完了');
              },
              error() {
    
              }
            })
          },
          error(){
            
          }
        })
      </script>
    </head>
    
    <body>
    
    </body>
    
    </html>
    

    相关文章

      网友评论

          本文标题:generator

          本文链接:https://www.haomeiwen.com/subject/qizmkqtx.html