1024programmer Java Using arguments in JavaScript to obtain the number of parameters passed by a function_javascript skills

Using arguments in JavaScript to obtain the number of parameters passed by a function_javascript skills

JS and PHP are slightly different in terms of function parameter passing. PHP’s formal parameters must match the number of actual parameters, but JS is much more flexible. You can pass parameters at will, and no error will be reported if the actual parameters are less or more than the formal parameters.

If there are more actual parameters than formal parameters, no error will be reported

 function say(a){
    alert(a);
 }
 
 say('Qiongtai Blog','WEB Technology Blog');

Execution results

Qiongtai Blog

Let’s take a look at the result of having more formal parameters than actual parameters

 function say(a,b){
    alert('a's value is '+a+'\nb's value is '+b);
 }
 
 say('Qiongtai Blog');

Execution results

arguments array in JS

a corresponds to the first actual parameter “Qiongtai Blog”, b has no corresponding actual parameter, so the value is undefined

arguments object

In fact, sometimes when the program design is more complex, we do not specify the number of parameters and use them flexibly. There is an array arguments in the function that specifically stores the actual parameter group. Through arguments we can know the number and value of the actual parameters.

 function arg(){
var str = 'A total of '+arguments.length+' parameters were passed
';
for(var i=0;i

Execution results

JS

In the above example, we defined the function arg without specifying formal parameters for it. Instead, we used the arguments object to receive actual parameters, which is very flexible.

For example, we can use it to calculate the smallest number in a set of numbers, no matter how many numbers there are in the set. Such as the following code:

 function arg(){
var tmp = 0, str = 'at';
for(var i=0;i

Execute the comparison results of four numbers: 200, 100, 59, 3500

arguments object in JS

We are adding two numbers, 5 and 60

 function arg(){
var tmp = 0, str = 'at';
for(var i=0;i

Execute the comparison results of six numbers: 200, 100, 59, 3500, 5, 60

arguments object in JS

Based on the results of the two operations, we found that no matter how many numbers we pass in, the results can be compared correctly. arguments are generally used where the number of actual parameters is variable. For example, in the above example, you can pass 5 numbers for comparison, or you can pass 100 numbers for comparison.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/629652

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索