2

Recently I became aware of ES6 iterator went to the help of JS arrays. I searched about it but still confused about its philosophy of design, because other methods was still OK.

On the other hand I've been told not to use it!

var arr = ['w', 'y', 'k', 'o', 'p'];
var eArr = arr[Symbol.iterator]();

// your browser must support for..of loop
// and let-scoped variables in for loops

for (let letter of eArr) {
  console.log(letter);
}

Was it just because of for..of support?

Of course, this feature hasn't been added to JS objects.

15
  • 2
    iterators are used for for/of and for generators. The whole concept of having lots of types (Array, Set, Map, spread syntax, rest syntax, any generator) that can all be iterated exactly the same way is really, really useful. It creates a generic mechanism that lots of things can use without having to design a new mechanism for every type. Commented Feb 20, 2017 at 22:33
  • 1
    Plus, you can now make any object type work with for/of and you can make it work any way you want by just creating an appropriate iterator or even multiple different iterators for your object. Commented Feb 20, 2017 at 22:36
  • 1
    If you are running in an environment where ES6 iterators are universally supported (such as recent versions of node.js or transpiling), then I've only seen recommendations to use iterators, not seen any recommendations to avoid them. For example, there's pretty much no reason to use .forEach() any more for iterating because for/of is simply better (provides more looping control without an extra function call and can be better optimized). Commented Feb 20, 2017 at 22:38
  • 1
    @jfriend00, is it true that for...of is always better optimised? I have seen very good performance with .forEach (or .some, and the likes), where the optimisation seemed to work very well. Commented Feb 20, 2017 at 22:40
  • 1
    @trincot - I know of no reason to use .forEach() any more in an ES6 world. You simply get better control with for/of. I don't know how much various interpreters have taken advantage of optimizing for/of, but it should have greater opportunities for optimization because the looping is entirely in the interpreter's control, whereas with .forEach() there's a method that is in control, not the interpreter. I use for/of because I have better looping control and forEach() offers nothing that for/of doesn't already have. Commented Feb 20, 2017 at 22:42

1 Answer 1

3

The reasons for introducing symbols like [Symbol.iterator] include that it allows one to define it also for custom objects, and so make them available for for ... of, spread syntax, and other constructs that can be used with iterables.

And as this possibility was made available, it was only logical to also implement it that way for standard objects that were to expose iterable behaviour, like arrays. This makes it all the more consistent: both standard and custom objects will now expose the iterable capability if, and only when, they define [Symbol.iterator].

Now methods (e.g. Array.from) and constructors (e.g. Set) become available which only require an iterable to be passed to them, which can be an array, but doesn't have to be. This opens up possibilities of lose coupling, where different libraries may even communicate based on this iterable protocol.

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

Comments

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.