简体   繁体   English

使用ES6制作​​迭代器

[英]Making an Iterator with ES6

This question has a lot of moving parts, but I'm going to start with the my first miscomprehension. 这个问题有很多动态的部分,但是我将从我的第一个误解开始。

Context: An Iterator is defined as an Object that implements a next() method, which both of my examples down below have. 上下文: Iterator被定义为实现next()方法的对象,下面的两个示例都具有该方法。 The difference is that I'm creating an Object with a next() method differently. 不同之处在于,我用不同的next()方法创建对象。 In my first example, I'm creating a function that returns an Object that contains a next() method. 在我的第一个示例中,我正在创建一个函数,该函数返回一个包含next()方法的Object。 I then assign a variable to this function, so if I'm not mistaken, I am essentially making an Object on the fly with a predefined function. 然后,我为该函数分配一个变量,因此,如果我没有记错的话,我实际上是使用预定义的函数动态地制作一个对象。

var letters = ["a","b","c"];

function createIterator(array) {
    var i = 0;

    return {                //return an Object with a next() method
        next: function(){
            i < array.length ?    //if statement
            {value: array[i++], done: false}:
            {value: undefined, done: true};
         }
    }
}

var myIterator = createIterator(letters);
console.log(myIterator.next()) //{value: a, done: false}
console.log(myIterator.next()) //{value: b, done: false}
console.log(myIterator.next()) //{value: c, done: false}
console.log(myIterator.next()) //{value: undefined, done: true}

So, by putting the createIterator function inside a variable, myIterator , each time I run the next() function, I get the next element in the array I pass. 因此,通过将createIterator函数放入变量myIterator ,每次运行next()函数时,我都会获得传递的数组中的下一个元素。

Okay, I thought. 好吧,我想。 What if I just made the Object without the function? 如果我仅使对象不具有功能怎么办?

var literal = {
    letters: ["a", "b", "c"],
    next: function(){        //same next function as before
        var i = 0;
        i < this.letters.length ? 
        {value: this.letters[i++], done: false}:
        {value: undefined, done: true};
         }
}

console.log(literal.next()) //{value: a, done: false}
console.log(literal.next()) //{value: a, done: false}
console.log(literal.next()) //{value: a, done: false}
console.log(literal.next()) //{value: a, done: false}

I think this is due to my misunderstanding of how I'm invoking the next() method with different ways of creating my Objects. 我认为这是由于我对我如何使用创建对象的不同方式调用next()方法的误解造成的。 It could be something with scope, but I'm really not entirely sure. 这可能与范围有关,但我确实不确定。

In the first example i is captured in a closure so it works. 在第一个示例中, i被捕获在一个闭包中,因此可以正常工作。 In the second example i gets created new every time you call the function. 在第二个示例中,每次调用该函数时, i都会被创建。 You could make is a property of the object: 可以使对象成为一个属性:

 var literal = { letters: ["a", "b", "c"], i: 0, next: function(){ //same next function as before return this.i < this.letters.length ? {value: this.letters[this.i++], done: false}: {value: undefined, done: true}; } } console.log(literal.next()) console.log(literal.next()) console.log(literal.next()) console.log(literal.next()) 

Of course you can also implement this in a way that it will work as an iterator in other contexts and is simpler: 当然,您也可以通过在其他上下文中将其用作迭代器并且更简单的方式来实现它:

 var G = { letters: ["a", "b", "c"], [Symbol.iterator]: function*(){ yield *this.letters } } // now the object works as an iterable: console.log([...G]) // or let iter = G[Symbol.iterator]() console.log(iter.next()) console.log(iter.next()) console.log(iter.next()) console.log(iter.next()) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM