Method for saving the arguments object using variables in JS, jsarguments
Iterator is an object that can access data sets in sequence. A typical API is the next method. This method is used to obtain the next value in the sequence.
Iterator example
Question: I want to compile a convenient function that can receive any number of parameters and create an iterator for these values.
Test code:
var it=values(,,,,,,,,);it.next();//it.next();//it.next();//
Analysis: because the values function needs to receive any number of parameters, we need to use the method used to build a variable parameter function described in the previous section. Then, the iterator object is used to traverse the elements of the arguments object.
Preliminary code
Function values () {var I =, n = arguments. length; return {hasNext: function () {return I <n ;}, next: function () {if (this. hasNext () {return arguments [I ++];} throw new Error ("reached ");}}}
Use the above test code for testing.
var it=values(,,,,,,,,);it.next();//undefinedit.next();//undefinedit.next();//undefined
Error Analysis
The code running result is incorrect. The initial encoding program is analyzed below.
Function values () {var I =, n = arguments. length; // no error. arguments is the built-in object return {hasNext: function () {return I <n ;}, next: function () {if (this. hasNext () {return arguments [I ++]; // The error occurs here. arguments is the built-in object of the next method function .} Throw new Error ("reached the end ");}}}
This refers to an error, which is like another headache. When processing the point of this, it is usually to use variables and save the correct this. Use this variable elsewhere. Then the arguments object solution will come out, and a variable will be used for storage, so that the arguments object reference will be no problem.
Encode again
Function values () {var I =, n = arguments. length, arg = arguments; return {hasNext: function () {return I <n ;}, next: function () {if (this. hasNext () {return arg [I ++];} throw new Error ("reached ");}}}
Run the test code
var it=values(,,,,,,,,);it.next();//it.next();//it.next();//
The results are the same as expected.
Prompt
When referencing arguments, beware of function nesting level
Binds a specific scope reference to the arguments variable, so that it can be referenced in nested functions.
Appendix 1: iterator
Iterator is also known as cursor. It is a software design pattern designed for programming and can traverse interfaces on containers. designers do not need to care about the container content.
UML class diagram of the iterator
Implementation of iterator js
I know a little about the design model, but there are many factory models in the specific project, and others are rarely used. Below is a simple implementation, which is not correct. Please contact us.
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,) ;}, iterator: function () {return new Iterator (this) ;}} function Iterator (list) {this. list = list; this. cur =;}; Iterator. prototype = {hasNext: function () {return this. cur <this. list. data. length-;}, next: function () {if (this. hasNext () {return this. lis T. data [this. cur ++];} throw new Error ('already exists ~ ');}, Remove: function () {this. list. remove (this. cur) ;}} var list = new List (); var it = list. iterator (); list. add (,); it. next (); // it. next (); // it. next ();//
The preceding section describes how to use variables to save the arguments object in JavaScript!