ES6 has .next (); Is there a way to get .previous ()?

Transition through the full ES6 table Compatibility table . Just moved on to Set().

var wow = new Set();
wow.add('foo');
wow.add('baz');
var iterator = set.values();
iterator.next(); // Object {value: "foo", done: false}
iterator.next(); // Object {value: "baz", done: false}

Question: How can I write a method similar to .next(), but it returns instead of forwarding?

Caution: I have nothing to do with this ... I am sure it has a smell of code. I'm just wondering how it all works.

+4
source share
2 answers

values()returns an iterator object, and you cannot repeat them, because JavaScript iterator objects can be infinite. For example, consider this

function * Counter() {
    "use strict";
    var i = 0;
    while (1) {
        yield i++;
    }
}

Counter(), . , .


- backIterator, , , next previous.

+4

, JS-, .

+1

All Articles