This article describes how to use variables to save the arguments object in JS. It is of great reference value. For more information, see 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
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; // there is no error here. arguments is the built-in object return {hasNext: function () {return I
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
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
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
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
The preceding section describes how to use variables to save the arguments object in JavaScript!
For more information about how to use variables to save the arguments object in JS, see PHP!