Home > Article > Web Front-end > 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 and PHP are slightly different in terms of function parameter passing. PHP's formal parameters must match the number of actual parameters, while Using arguments in JavaScript to obtain the number of parameters passed by a function_javascript skills 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.
No error will be reported if there are more actual parameters than formal parameters
function say(a){ alert(a); } say('Using arguments in JavaScript to obtain the number of parameters passed by a function_javascript skills','WEB技术博客');
Execution results
Let’s take a look at the result of having more formal parameters than actual parameters
function say(a,b){ alert('a 的值是 '+a+'\nb 的值是 '+b); } say('Using arguments in JavaScript to obtain the number of parameters passed by a function_javascript skills');
Execution results
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 is specially used to store the actual parameter group. Through arguments we can know the number and value of the actual parameters.
function arg(){ var str = '总共传了'+arguments.length+'个参数 '; for(var i=0;i<arguments.length;i++){ str += '第'+(i+1)+'个参数值:'+arguments[i]+'\n'; } alert(str); } arg('Using arguments in JavaScript to obtain the number of parameters passed by a function_javascript skills','PHP博客','WEB技术博客');
Execution results
In the above example, we defined the function arg and did not specify 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 = '在 '; 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.length-1)+' 里最小的值是 '+tmp); } arg(200,100,59,3500);
Execute the comparison results of four numbers 200, 100, 59, 3500
We are adding two numbers, 5 and 60
function arg(){ var tmp = 0, str = '在 '; 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.length-1)+' 里最小的值是 '+tmp); } arg(200,100,59,3500,5,60);
Execute the comparison results of six numbers: 200, 100, 59, 3500, 5, 60
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 example above, you can pass 5 numbers for comparison, or you can pass 100 numbers for comparison.