Deep understanding of the JavaScript series (35): Design pattern of the iterator model _javascript tips

Source: Internet
Author: User
Tags rewind

Introduced

Iterator mode (iterator): Provides a way to order the elements of an aggregate 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.
2. Provides a unified interface for traversing different collection structures, thus supporting the same algorithm to operate on different set structures.
3. Changing the structure of the iterator at the same time as traversing can cause problems (for example, C # 's foreach does not allow the item to be modified).

Body

In general iterations, we should have at least 2 methods, Hasnext () and next (), so that we can iterate through all the objects, we first give an example:

Copy Code code as follows:

var agg = (function () {
var index = 0,
data = [1, 2, 3, 4, 5],
length = Data.length;

    return {
        next:function () {
             var element;
            if (!this.hasnext ()) {
                 return null;
           }
            element = Data[index];
            index = index + 2;
            return element;
       },

Hasnext:function () {
return index < length;
},

Rewind:function () {
index = 0;
},

Current:function () {
return Data[index];
}

};
} ());


The method is the same as in normal C #:
Copy Code code as follows:

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 extra way, and then move on to another operation:
Copy Code code as follows:

Reset
Agg.rewind ();
Console.log (Agg.current ()); 1

jquery Application Examples

A very famous iterator in jquery is the $.each method, through which we can pass in additional function, and then iterate over all item items, such as:

Copy Code code as follows:

$.each ([' Dudu ', ' Dudu ', ' yogurt sister ', ' that mm '], function (index, value) {
Console.log (index + ': ' + value);
});
Or
$ (' Li '). each (function (index) {
Console.log (index + ': ' + $ (this). text ());
});

Summarize

The use scenario for an iterator is that we can use an iterator pattern when we don't want to expose the internal structure of a collection, but we have to let the client code access the elements in a transparent base.

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.