0

In the notes it states:

The iterable protocol allows JavaScript objects to define or customize their iteration behavior, such as what values are looped over in a for..of construct.

I don’t see what benefit this has when I can already use: Object.degineProperty to make something enumerable.

function withValue(value) {
       var d = withValue.d || (
           withValue.d = {
               enumerable: false,
               writeable: false,
               configuration: false,
               value: null
          }
      )
    // other code;
   }

What benefit do these protocols have? If this is just some new syntax to appease the new for…of loop, what benefit does it have other than simply checking the length and seeing if its ran out of items in the “list”.

3
  • Enumerability of properties has absolutely nothing to do with iterators? Most importantly, properties have no order in which to be iterated. Commented Jul 30, 2017 at 17:05
  • An iterator is a much more generic thing than a "list". It doesn't even need to have a length - it can be infinite if you want! Commented Jul 30, 2017 at 17:06
  • "In the notes it states" - what? where? which notes? Commented Jul 30, 2017 at 17:21

1 Answer 1

1

Think of Iterable as an interface. You can be assured implementations contain an Symbol.iterator property, which implements a next() method. If you implement yourself, you can produce the values you want to iterate over at runtime. As a simple example, produce a list and decide later how many (or which, or whatever criteria) you would like to iterate over:

function List (...args) {
    this.getOnly = function (limit) (
        const effectiveLimit = Math.min(args.length, limit + 1);
        const iterable = {
            [Symbol.iterator]() {
                let count = 0;
                const iterator = {
                    next() {
                        if (count < effectiveLimit) {
                            return { value: args[count++] };
                        } else {
                            return { done: true };
                        }
                    }
                };
                return iterator;
            }
        }
        return iterable;
    };
}

const list = List(0, 1, 2, 3, 4);
for (const x of list.getOnly(3)) {
    console.log(x);
}
// returns 0, 1, 2

If you use a Generator function, which implements the Iterable interface, the same gets really simple:

function List (...args) {
    this.getOnly = function* (limit) {
        const effectiveLimit = Math.min(args.length, limit + 1);
        for (let count = 0; count < effectiveLimit; count++) {
            yield args[count];
        }
    }
}

More examples of what you can do with Iterables are listed here.

Sign up to request clarification or add additional context in comments.

1 Comment

The count should be declared inside the iterator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.