PHP has a handy use when defining a function to set a default value directly to the parameter, such as:
function
simue ($a =1, $b =2) {
return $a + $b;
}
Echo Simue (); Output 3
Echo Simue (10); Output 12
echo Simue (10,20); Output
But JS can not be so defined, if write function Simue (a=1,b=2) {} will prompt the missing object.
The JS function has an array of stored parameters arguments , and the parameters obtained by all functions are saved to the array by the compiler. So our JS version of the support parameter default value of the function can be implemented in another way, modify the above example:
function
Simue (){
var a = Arguments[0]? Arguments[0]: 1;
var B = arguments[1]? ARGUMENTS[1]: 2;
return a+b;
}
Alert (Simue ()); Output 3
Alert (Simue (10)); Output 12
Alert (Simue (10,20)); Output
The default value problem for JS's function parameter