One, you can pass any number of parameters
When invoking a function, you can pass as many arguments as you want, regardless of how many formal arguments the function declares. The value of the missing parameter is undefined, and the extra parameters are ignored directly.
Second, all parameters passed are stored in arguments.
All of the passed parameters are stored in a very special variable, much like an Array (which continues to see why)--arguments.
Let's look at how this variable is used by the following function:
function g () { console.log (' Length: ' +arguments.length); Console.log (' Elements: ' +FromArray (arguments));//FromArray function, which converts the arguments into an array so that it can be stored in the data}
Call G ():
>0Elements:> G (' A '1elements:a> G (' A ', ' B '2elements:a,b
No matter how many parameters are explicitly declared, arguments is always there, and it contains all the actual arguments.
If the caller does not provide a parameter, it is undefined
passed to function
. You do not pass the argument with the undefined
result of the incoming obtained is the same
而传入
false
, 0
and empty strings are parsed as missing parameters
Tips:
We can use it ||
to specify the default value of the parameter:
function add (x, y) {
x = x | | 0;
y = y | | 0;
return x + y;
}
Fundamentals of parameter processing