【问题标题】:What is `it` here in the code?代码中的“it”是什么?
【发布时间】:2017-03-22 22:48:55
【问题描述】:

我正在阅读以下blog

他们在博客中有代码:

function request(url) {
    // this is where we're hiding the asynchronicity,
    // away from the main code of our generator
    // `it.next(..)` is the generator's iterator-resume
    // call
    makeAjaxCall( url, function(response){
        it.next( response );
    } );
    // Note: nothing returned here!
}

代码看起来很简单。但是在makeAjaxCall 回调中,他们有一个名为it.next(response) 的行。这是什么it?我知道他们说它来自生成器,但我没有看到它在任何地方传递!

但他们这样做:

function *main() {
    var result1 = yield request( "http://some.url.1" );
    var data = JSON.parse( result1 );

    var result2 = yield request( "http://some.url.2?id=" + data.id );
    var resp = JSON.parse( result2 );
    console.log( "The value you asked for: " + resp.value );
}

这行怎么来的:

var result1 = yield request( "http://some.url.1" );

如果itrequest 函数中未定义,是否有效?

【问题讨论】:

  • 它是Iterator。您必须在其他地方定义它
  • // it.next(..) is the generator's iterator-resume // call 所以it is the generator's iterator
  • it 在完全相同的代码块中定义。 var it = main();
  • @batman,你find it了吗?

标签: javascript ecmascript-6 generator


【解决方案1】:

it 是一个生成器,它在底部的博文中定义:

var it = main();
it.next(); // get it all started

【讨论】:

    【解决方案2】:

    来自您所说的那个博客中的同一位作者,之前有一篇文章解释了 ES6 生成器的基础知识。你可以找到之前的帖子here。关于你的问题,他在同一篇文章的第一段中这样定义:

    function *foo() {
        yield 1;
        yield 2;
        yield 3;
        yield 4;
        yield 5;
    }
    
    var it = foo();
    

    你应该阅读那篇文章,它一步一步地解释了一切:)

    【讨论】:

      【解决方案3】:

      页面上的完整示例:

      function request(url) {
          // this is where we're hiding the asynchronicity,
          // away from the main code of our generator
          // `it.next(..)` is the generator's iterator-resume
          // call
          makeAjaxCall( url, function(response){
              it.next( response );
          } );
          // Note: nothing returned here!
      }
      
      function *main() {
          var result1 = yield request( "http://some.url.1" );
          var data = JSON.parse( result1 );
      
          var result2 = yield request( "http://some.url.2?id=" + data.id );
          var resp = JSON.parse( result2 );
          console.log( "The value you asked for: " + resp.value );
      }
      
      var it = main();
      it.next(); // get it all started
      

      it 是一个iterator -(main 函数)。

      关于Iterators and generators的参考链接。

      关于迭代器的示例:

      function makeIterator(array){
          var nextIndex = 0;
      
          return {
             next: function(){
                 return nextIndex < array.length ?
                     {value: array[nextIndex++], done: false} :
                     {done: true};
             }
          }
      }
      
      var it = makeIterator(['yo', 'ya']);
      console.log(it.next().value); // 'yo'
      console.log(it.next().value); // 'ya'
      console.log(it.next().done);  // true
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-01
        • 2017-06-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多