What is the technical definition of Javascript iteratively and how do you test it?

I am implementing a useful subclass of an ES6 object Set. For many of my new methods, I want to accept an argument that can be either another Set or Array, or really any that I can repeat. I call it β€œiterable” in my interface and just use .forEach()it (which works fine for Set or Array. Code example:

// remove items in this set that are in the otherIterable
// returns a count of number of items removed
remove(otherIterable) {
    let cnt = 0;
    otherIterable.forEach(item => {
        if (this.delete(item)) {
            ++cnt;
        }
    });
    return cnt;
}

or

// add all items from some other iterable to this set
addTo(iterable) {
    iterable.forEach(item => {
        this.add(item);
    });
}

But I suspect that perhaps I do not support any iterability in how ES6 defines it, so I am interested in the fact that a true definition of iterative Javascript uses this term as an ES6 specification?

How do you test it in ES6 Javascript?

How should you repeat common repeatable?

I found such phrases in the ES6 specification:

iterable , , @@iterator, , , WeakMap .

@@iterator method, , , .

+4
2

Javascript, , ES6?

Β§25.1.1.1 " ".

Symbol.iterator -keyed, (, , , , , , , Β§25.1.1.2).

ES6 Javascript?

, @@iterator, , , Iterator, .

function looksIterable(o) {
    return typeof o[Symbol.iterator] == "function";
}

, , .

?

forEach. ( forEach ES6).

- for (… of …). ( GetIterator ( ) , TypeError .

+6

Symbol Symbol.iterator, , , , "" "@@iterator". , :

object[Symbol.iterator] = function* () {
  // do something that makes sense
};

, - ,

if (Symbol.iterator in object)

(, , , ). ( Symbol.iterator) ( *, ). , , , .next(). value done.

for ... of "spread" ... .

+1

Source: https://habr.com/ru/post/1666323/


All Articles