The arguments object in JavaScript

Source: Internet
Author: User

In JS, everything is an object, and even a function is an object, and the function name is actually a variable referencing the function definition object.

1. What is arguments?

The arguments in this function body is very special, and is actually a built-in class array object of the function, which can be used with the [i] and. Length of the array.

2. What is the role?

JS syntax does not support overloading! However, you can simulate overloading effects with arguments objects.

Arguments object: Within the Function object, automatically creates an array object that is specifically to receive all the arguments that are worth the class.
Arguments[i]: Gets the parameter value of the incoming subscript I
Arguments.length: Get the number of arguments passed in!

Overload:

In a program, you can define multiple functions with the same function name, different parameter lists,
The caller does not have to distinguish between the arguments of each function
At execution time, the program automatically determines which function executes based on the number of parameters passed in.

Examples are as follows:

// 1. If the user passes in a parameter, the square    function sum (a) {        console.log (A *a);    }     // If a user passes two parameters, the sum    function sum (b) {        Console.log (a+b);    }    SUM (//?)     //? 

The intent of the above example is to have the function sum () of the same name output different results depending on the parameters, but sum is the function name and the essence is a variable.

The second one overrides the first one, so the correct output answer is: nan,9. So obviously not.

If you use arguments, it's a lot easier.

Here are 2 examples:

//2.    functionCalc () {//If the user passes in a parameter, the square        if(arguments.length==1) {Console.log (arguments[0]*arguments[0]); }Else if(arguments.length==2){        //If a user passes two parameters, the sumConsole.log (arguments[0]+arguments[1]); }} Calc (4);// -Calc (4,5);//9
/*3. No matter how many numbers a user passes in, they can sum*/    functionAdd () {//arguments:[]        //iterate through each element in the arguments and accumulate         for(vari=0,sum=0;i<arguments.length;sum+=arguments[i++]); returnSum//Return and} console.log (Add ());//6Console.log (Add (1,2,3,4,5,6));// +

This is the effect of JS using arguments overload, a simple understanding is a function of re-use.

Arguments.length is the decision to have an argument, that is, the number of arguments inside the function call!

The arguments object in JavaScript

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.