_javascript Techniques for arguments objects in JavaScript

Source: Internet
Author: User
Tags function definition

In JS everything is an object, the function is also an object, function name is actually refers to function definition object variables.

1, what is arguments?

The arguments in this function is very special, and is actually a built-in class array object for 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 overloaded effects with arguments objects.

Arguments object: A function object that is automatically created to receive all parameter-worthy array objects.
Arguments[i]: Gets the parameter value of the incoming subscript I
Arguments.length: Get the number of parameters passed in!

Overload:

In a program, you can define multiple functions with the same function name, different parameter list,
The caller does not have to distinguish between arguments for each function,
When executed, the program automatically determines which function to perform according to 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) is evaluated;
If the user passes in two parameters, sum
function sum (a,b) {
console.log (a+b);
}
SUM (4); ?

In the example above, the idea is to have the same function sum () different output depending on the parameters, but sum is the function name, the essence is a variable,

The second one will overwrite the first, so the correct output of the above answer is: nan,9. So it's obviously not possible.

If you use arguments, it's much simpler.

Here are 2 examples:

2.
Function Calc () {
//If the user passes in a parameter, ask the square
if (arguments.length==1) {
Console.log (arguments[0]* Arguments[0]);
} else if (arguments.length==2) {
//If the user passes in two arguments, Sum
console.log (arguments[0]+arguments[1]);
}
Calc (4);
Calc (4,5);//9 
/*3, no matter how many numbers a user passes in, can sum/
function Add () {
//arguments:[]
// Iterate through each element of the arguments and accumulate for
(var i=0,sum=0;i<arguments.length;sum+=arguments[i++]);
Return sum;//and
}
console.log (Add (1,2,3));//6

This is the effect of JS using arguments overload, simple understanding is a function reuse.

Arguments.length is the decision of the argument, that is, the number of parameters inside the function call!

The above is a small set of JavaScript to introduce the arguments object of the relevant knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.