In-depth understanding of the JavaScript series (35): Design pattern iterator mode

Source: Internet
Author: User

describes the iterator pattern (Iterator): Provides a way to order individual elements in an aggregation object without exposing the internal representation of the object. Several features of iterators are:1. Access the contents of an aggregated object without exposing its internal representation. 2provides a unified interface for traversing different collection structures, enabling the same algorithm to operate on different sets of structures. 3the traversal of changing the collection structure at the same time as the iterator may cause problems (such as in C # 's foreach does not allow the modification of item). The general iteration of the body, we have at least 2 methods, Hasnext () and next (), so as to do the traversal of all objects, we first give an example:varagg = (function () {    varindex = 0, the data= [1, 2, 3, 4, 5], length=data.length; return{Next:function () {            varelement; if(! This. Hasnext ()) {                return NULL; } element=Data[index]; Index= index + 2; returnelement; }, Hasnext:function () {            returnIndex <length; }, Rewind:function() {index= 0; }, Current:function () {            returnData[index]; }    };} ()); The method of use is the same as in C # in the usual way://The result of the iteration is: 1,3,5 while(Agg.hasnext ()) {Console.log (Agg.next ());} Of course, you can also reset the data in an additional way, and then continue with the other actions:// ResetAgg.rewind (); Console.log (Agg.current ());//1jquery Application Example a very well known iterator in jquery is the $.each method, through which we can pass in additional function and then iterate over all item items, for example: $.each ([' Dudu ', ' Dudu ', ' yogurt sister ', ' that mm ',function(Index, value) {Console.log (index+ ': ' +value);});//or$ (' Li '). each (function(Index) {console.log (index+ ': ' + $ ( This). text ());}); The usage scenario for a summary iterator is that the internal results of the collection are often different, we don't want to expose the internal structure, but we can use the iterator pattern when the client code transparently accesses the elements. Synchronization and recommendation this article has been synchronized to the directory index: in-depth understanding of JavaScript series in-depth understanding of the JavaScript series, including the original, translation, reprint and other types of articles, if it is useful to you, please recommend supporting a, to the power of the uncle writing. 

In-depth understanding of the JavaScript series (35): Design pattern iterator mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.