Deep understanding of JavaScript series (35): Design Pattern-iterator Pattern

Source: Internet
Author: User
Introduction

Iterator: provides a method to sequentially aggregate elements of an object without exposing the internal representation of the object.

The iterator has the following features:

    1. Access the content of an aggregate object without exposing its internal representation.
    2. Provides a unified interface for Traversing different set structures to support the sameAlgorithmOperate on different collection structures.
    3. Changing the structure of the set where the iterator is located during traversal may cause problems (for example, the item cannot be modified in C #'s foreach ).
Body

In general iteration, we must have at least two methods, hasnext () and next (), to traverse all objects. Let's give an example:

 VaR Vertex = ( 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 usage is the same as that in C:

//The result of the iteration is: 1, 3, 5
While(Iterator. hasnext ()){
Console. Log (summary. Next ());
}

Of course, you can reset the data in an additional way and continue with other operations:

 
//Reset
Response. Rewind ();
Console. Log (summary. Current ());//1

Jquery application example

A very famous iterator in jquery is the $. Each method. With each, we can pass in additional functions and perform iterative operations on all item items, for example:

$. Each (['dududu', 'dududu', 'yogurt girl ', 'Mm'],Function(Index, value ){
Console. Log (index + ':' + value );
});
//Or
$ ('Lil'). Each (Function(Index ){
Console. Log (index + ':' + $ (This). Text ());
});

Summary

The application scenario of the iterator is: the internal results of the set are often different. If we don't want to expose the internal structure of the set, but it makes the customerCodeThe transparent bottom accesses the elements. In this case, we can use the iterator mode.

Transferred from: Uncle Tom

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.