Use arguments in JavaScript to obtain the number of parameters passed by the function.

Source: Internet
Author: User

Use arguments in JavaScript to obtain the number of parameters passed by the function.

JavaScript and PHP are a little different in parameter passing through functions. PHP is used to match the number of real parameters, while JS is much more flexible. You can freely transmit parameters, no error is reported if the actual parameter is less or more than the actual parameter.

No error is reported if the actual parameter is more than the actual parameter.

Function say (a) {alert (a);} say ('qiongtai blog ', 'web technology blog ');

Execution result

Let's take a look at the results with more parameters than the real parameters.

Function say (a, B) {alert (the value of 'a is '+ a +' \ the value of nb is '+ B);} say ('qiongtai blog ');

Execution result

A corresponds to the first real parameter "qiongtai blog", and B does not have the corresponding real parameter, so the value is undefined.

Arguments object

In fact, sometimes we do not specify the number of parameters when the program design is complicated, and they are all flexible. In the function, there is an array arguments that stores the array of real parameters. With arguments, we can know the number and value of real parameters.

Function arg () {var str = 'total passed '+ arguments. length + 'parameter \ n'; for (var I = 0; I <arguments. length; I ++) {str + = 'dide' + (I + 1) + 'parameter values:' + arguments [I] + '\ n ';} alert (str);} arg ('qiongtai blog ', 'php blog', 'web technology blog ');

Execution result

In the above example, the arg function is defined to receive real parameters instead of specifying them. Instead, it is very flexible to use the arguments object to receive real parameters.

For example, we can use it to calculate the smallest number in a group of numbers, regardless of the number of these numbers. Run the following code:

Function arg () {var tmp = 0, str = 'in'; for (var I = 0; I <arguments. length; I ++) {for (var g = 0; g <arguments. length; g ++) {if (arguments [g] <arguments [I]) {tmp = arguments [g] ;}str + = arguments [I] + ', ';} alert (str. substr (0, str. the smallest value in length-1) + 'is' + tmp);} arg (200,100 );

Compare the result with the number of 200,100

We are adding two numbers: 5 and 60.

Function arg () {var tmp = 0, str = 'in'; for (var I = 0; I <arguments. length; I ++) {for (var g = 0; g <arguments. length; g ++) {if (arguments [g] <arguments [I]) {tmp = arguments [g] ;}str + = arguments [I] + ', ';} alert (str. substr (0, str. the smallest value in length-1) + 'is' + tmp);} arg (200,100 );

Execute 200,100, and 60 to compare the results.

Based on the two calculation results, we found that no matter how many numbers we pass in, we can correctly compare the results. Arguments is generally used in a place where the number of real arguments is not fixed. For example, in the preceding example, you can import 5 numbers for comparison, or upload 100 numbers for comparison.


Javascript: How do I pass the corresponding parameters when calling other functions?

In js, there is a way to get all the parameters passed to the function using paramters (forgot to write it) (in the array form). You can call the corresponding array when calling it.

It should meet your requirements.

Upstairs, let's talk about it. I have never figured out what is going on. The two functions

Javascript receives the function parameter this object in the function.

Learn more

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.