Detailed description of arguments objects in Javascript and detailed description of javascript

Source: Internet
Author: User
Tags addall

Detailed description of arguments objects in Javascript and detailed description of javascript

In the previous article, we discussed the default parameters in javascript. In this article, we will discuss the arguments parameter objects in javascript.

In the following example, how do we handle different functions based on different input parameters?

Copy codeThe Code is as follows:
Function addAll (){
// What do we do here?
}

// Shocould return 6
AddAll (1, 2, 3 );

// Shocould return 10
AddAll (1, 2, 3, 4 );

Fortunately, javascript has an arguments object that can handle the above situation. The arguments object is a class array object. To learn more about the arguments object, click here. We use the arguments object to change the previous example:

Copy codeThe Code is as follows:
Function addAll (){
Var sum = 0;

For (var I = 0; I <arguments. length; I ++ ){
Sum + = arguments [I];
}

Return sum;
}

// Returns 6
AddAll (1, 2, 3 );

// Returns 10
AddAll (1, 2, 3, 4 );

As mentioned above, the arguments object is a class array object. Let's test it below:

Copy codeThe Code is as follows:
Function getName (){
Console. log (Array. isArray (arguments ));
}

// Will output false
GetName ("benjamin ");

The test results show that:
It is not an array object. What is the difference between it and an array object? Click here for details.

The following example will throw an error:

Copy codeThe Code is as follows:
Function sortArgs (){
// Uncaught TypeError: undefined is not a function
Sorted = arguments. sort ()

Return sorted;
}
SortArgs ();

We can convert a class array object to an array object as follows:

Copy codeThe Code is as follows:
Function sortArgs (){
// Convert arguments object into a real array
Var args = []. slice. call (arguments );

// Now this will work!
Sorted = args. sort ()

Return sorted;
}

// Will output [1, 2, 3]
Console. log (sortArgs (1, 3, 2 ));

If you feel this article is helpful to you and want to transfer it to more people in need. If the article is inappropriate, please leave a message.


What is the arguments object in JS?

Refer to my js notes

In the function body, the identifier arguments is a reference to the real parameter object, and the real parameter object is a class array object arguments [0], arguments. length

What is arguments?
Answer: 1: arguments is a copy of the received arguments.
In lexical analysis, the attributes of AO are first formed based on the form parameters. The value is undefined.
When real parameters are passed, modify the corresponding attributes of AO.
2: Collect all received arguments and put them in an arguments object.
T (a, B, c ){},
Call time: t (1, 2, 3, 4, 5) 5 parameters
At this time, the AO attribute only has three attributes: a, bc, and arguments, which have values of 1, 2, 3, 4, and 5.

You can use arguments to obtain arguments that exceed the number of parameters.
3: The arguments index increments from 0, 1, 2,..., and corresponds to the real parameters one by one.
4: The arguments. length attribute indicates the number of real parameters.
5: arguments must not be an array. It is a long object like an array, although it also has the length attribute.
6: each function of arguments will have it. Therefore, arguemets will only find its own arguments internally,
The arguments of the outer layer cannot be referenced.
<Script type = "text/javascript">
// Calculate the circular area, rectangular area, and triangular area.
Function area (){
If (arguments. length = 1 ){
Alert (3.14 * arguments [0] * arguments [0]);
} Else if (arguments. length = 2 ){
Alert (arguments [0] * arguments [1]);
} Else if (arguments. length = 3 ){
Alert (arguments [0] + arguments [1] + arguments [2]);
} Else {
Return null;
}
}
Area (10, 20, 30 );
  
</Script>

Problems with arguments in Javascript Functions

Arguments is a pseudo array. The for in traversal object may be different in different browsers. Therefore, it is better to use the traditional

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.