How to save the arguments object using variables in JS

Source: Internet
Author: User
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!

Related Article

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.