Arguments object in Javascript, javascriptarguments

Source: Internet
Author: User

Arguments object in Javascript, javascriptarguments

In js, everything is an object, and even a function is an object. A function name is actually a variable that references a function definition object.

1. What is arguments?

The arguments in this function is very special. It is actually a built-in class array object of the function. You can use the [I] And. length arrays.

2. What is the function?

Js syntax does not support overloading! However, the arguments object can be used to simulate the Overload effect.

Arguments object: An array object automatically created in the function object to receive all parameter values.
Arguments [I]: Obtain the parameter value with the input subscript I.
Arguments. length: gets the number of input parameters!

Overload:

Multiple Functions with the same name and different parameter lists can be defined in the program,
The caller does not have to distinguish the parameters of each function,
During execution, the program automatically determines the function to be selected based on the number of input parameters.

Example:

// 1. If you input a parameter, calculate the square function sum (a) {console. log (a * a);} // If you input two parameters, sum function sum (a, B) {console. log (a + B);} sum (4 );//? Sum (4, 5 );//?

In the above example, we originally wanted the same name function sum () to output different results based on different parameters, but sum is the function name and essentially a variable,

The second will overwrite the first one, so the correct output is NaN, 9. So obviously this is not acceptable.

If arguments is used, it is much simpler.

Two examples are as follows:

// 2. function calc () {// if you input a parameter, obtain the square if (arguments. length = 1) {console. log (arguments [0] * arguments [0]);} else if (arguments. length = 2) {// If you specify two parameters, sum them to the console. log (arguments [0] + arguments [1]) ;}} calc (4); // 16 calc (4, 5 ); // 9/* 3. You can sum up multiple numbers */function add () {// arguments: [] // traverse each element in arguments, and accumulate for (var I = 0, sum = 0; I <arguments. length; sum + = arguments [I ++]); return sum; // return and} console. log (add (1, 2, 3); // 6console. log (add (1, 2, 3, 4, 5, 6); // 21

This is the effect of JS's use of arguments overload. A simple understanding is the reuse of a function.

Arguments. length is determined by real parameters, that is, the number of parameters in the function call is determined!

The above section describes the knowledge of arguments objects in Javascript. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.