The iterator pattern refers to providing a way to sequentially access individual elements in an aggregated object without exposing the object's internal representation.
The Array.prototype.foreach in JavaScript.
Iterators in 1.JQuery
$.each function
2. Implement each function yourself
var function (ary,callback) { for (var i=0,l=ary.length;i<l;i++) { Callback.call (ary[ I],i,ary[i]); // pass subscripts and elements as parameters to the callback function } }; each ([n/A],function(i,n) { alert ([I,n]); });
3. Internal iterators and external iterators
An internal iterator is very handy when it is called, and the outside of the iterator is not concerned with the internal implementation of iterators, and the interaction with iterators is only an initial call, but this is also the disadvantage of an internal iterator. Because the iterative rules of the internal iterators have been stipulated well in advance, some requirements are not well implemented.
An external iterator must explicitly request an iteration of the next element.
External iterators increase the complexity of some calls, but they also enhance the flexibility of iterators, and we can manually control the process or order of iterations.
4. The iterator pattern can not only iterate over algebraic groups, but also iterate over the objects of some class arrays.
In JavaScript, the for-in statement can be used to iterate over the properties of an ordinary literal object.
5. Reverse Iterator
6. Abort iterator
7. Application examples
8. Summary
The iterator pattern is a relatively simple pattern that we don't think is a design pattern in many cases.
JavaScript Design patterns and development Practices---reading notes (7) Iterator mode