Parameters of the JavaScript function: formal parameter, actual parameter
//function Parameters //formal parameter list functionTest (a,b,c,d) {//alert (test.length); Number of parameters, 4 //The actual argument of the function, which is to use an array to receive the actual parameters of the function //arguments object that can access the actual parameters of the function
Arguments objects can only be used inside a function
alert (arguments.length);//2 xAlert (arguments[0]);//TenAlert (arguments[1]);// - returnA +b; } alert (Test (10,20));//30, the parameters of the function and the actual parameter are inconsistent without error.
However, this program is not rigorous, we hope that only the physical parameters and the number of arguments consistent with the time to execute the function, or throw an exception or give a hint
So add judgment:
if (Test.length = = arguments.length) {return a +b; } Else { return ' parameter is incorrect!} ';}
There are problems, although test.length can take the number of formal parameters, but generally not so use, there will be potential problems. General use Arguments.callee Example:
// recursive function fact (num) { ifreturn 1; Else return num*fact (num-1); } var F = fact; // you assign the fact to the variable F, which is equivalent to copying a copy of the null; // Suppose someone else had made the fact null, Alert (F (5)); Error
But using Arguments.callee instead of fact would not be an error:
//Recursive functionfact (num) {if(num<=1)return1; Else returnNum*arguments.callee (num-1);//using Arguments.callee, point to the function itself } varF = fact;//you assign the fact to the variable F, which is equivalent to copying a copy of theFact =NULL;//Let's say someone else made the fact null.Alert (F (5));
So the upper use of the parameter is not test.length, also use arguments.callee.length:
if (Arguments.callee.length = = arguments.length) {return a +b;} Else { return ' parameter is incorrect!} ';}
Summarize:
Tenth--------JavaScript functions-parameters