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.
for/ofand 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.for/ofand you can make it work any way you want by just creating an appropriate iterator or even multiple different iterators for your object..forEach()any more for iterating becausefor/ofis simply better (provides more looping control without an extra function call and can be better optimized).for...ofis always better optimised? I have seen very good performance with.forEach(or.some, and the likes), where the optimisation seemed to work very well..forEach()any more in an ES6 world. You simply get better control withfor/of. I don't know how much various interpreters have taken advantage of optimizingfor/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 usefor/ofbecause I have better looping control andforEach()offers nothing thatfor/ofdoesn't already have.