An iterator (iterator) is an object that can sequentially access a collection of data. One of its typical APIs is the next method. The method obtains the next value in the sequence.
Iterator Example title: You want to write a handy function that can receive any number of arguments and create an iterator for those values.
Test the code well:
var it=values (1,4,1,4,2,1,3,5,6); It.next ();//1it.next ();//4it.next ();//1
Analysis: Because the values function needs to receive any number of parameters, here is the method used to construct the variable parameter function in the previous section. The iterator object inside then iterates through the elements of the arguments object. Preliminary code
function values () { var i=0,n=arguments.length; return { hasnext:function () { return i<n; }, next:function () { if (This.hasnext ()) { return arguments[i++]; } throw new Error ("reached last");}} }
Test with the test code above
var it=values (1,4,1,4,2,1,3,5,6); It.next ();//undefinedit.next ();//undefinedit.next ();//undefined
Error analysis
The code does not run correctly, and the initial encoding program is analyzed below.
function values () { var i=0,n=arguments.length;//there is no error here, arguments is the built-in object in values return { hasnext: function () { return i<n; }, next:function () { if (This.hasnext ()) { return arguments[i++];// Error appears here, arguments is the built-in object of the next method function. } throw new Error ("reached last");}} }
The reference error here is much like another headache for this one. When you handle the point of this, you typically use variables and save the correct this. This variable is then used elsewhere. Then the solution to the arguments object comes out, with a variable to store, so that the arguments object's reference is no problem.
Encode again
function values () { var i=0,n=arguments.length,arg=arguments; return { hasnext:function () { return i<n; }, next:function () { if (This.hasnext ()) { return arg[i++]; } throw new Error ("reached last");}} }
Run the test code
var it=values (1,4,1,4,2,1,3,5,6); It.next ();//1it.next ();//4it.next ();//1
The results are the same as expected.
Tips
Appendix I: Iterators
Iterators (iterator), sometimes called cursors, are software design patterns for programming, interfaces that can be traversed on a container, and designers need not care about the contents of the container.
Iterator UML class diagram
Iterator JS Implementation
The design pattern to understand a little bit, but the specific project, there is more than the factory model, the other rarely used, the following is a simple implementation, the wrong place, welcome to communicate.
The code is as follows
function List () { this.data=[];} list.prototype={ add:function () { var args=[].slice.call (arguments) this.data=this.data.concat (args) ; }, remove:function (i) { this.data.splice (i,1); }, iterator:function () { return new Iterator (this);} } function Iterator (list) { this.list=list; This.cur=0;};i terator.prototype={ hasnext:function () { return this.cur<this.list.data.length-1; }, next : function () { if (This.hasnext ()) { return this.list.data[this.cur++]; } throw new Error (' already in the end ~ '); }, remove:function () { this.list.remove (this.cur);} } var list1=new List (); var it=list1.iterator (); List1.add (3,2,3,4,1,6,10,8,9); It.next ();//3it.next ();//2it.next ();// 3
[Effective JavaScript notes] 24th: Saving arguments objects with variables