One, iterator pattern definition
The iterator pattern provides a way to sequentially access individual elements in an aggregated object without exposing the internal representation of the object.
In most of the current languages, the iterator pattern is already built in. Iterators have internal iterators and external iterators, and most of the implementations in the existing language are mostly internal iterators.
Ii. each implementation in jquery
//Each iterator similar to jquery$.each=function (obj,callback) {varvalue, I=0, IsArray=isarraylike (obj), length=obj.length; if(IsArray) { for(i=0, i<length;i++) {Value=Callback.call (Obj,i,obj[i]); if(value==false){ Break; } } }Else{ for(Iinchobj) {Value=Callback.call (Obj,i,obj[i]); if(value==false){ Break; } } } returnobj; };
A city external iterator implementation
varIterator=function (obj) {varCurrent=0; varnext=function () { current+=1; }; varIsdone=function () {returncurrent>=obj.length; }; vargetcurrentitem=function () {returnObj[current]; }; return{next:next, isdone:isdone, Getcurrentitem:getcurrentitem}; };
JavaScript Design Pattern Learning seven--iterator mode