There is a very convenient usage of php that can directly set default values for parameters when defining functions, such as:
The code copy is as follows:
function simue ($a=1,$b=2){
return $a+$b;
}
echo simue(); //Output 3
echo simue(10); //Output 12
echo simue(10,20); //Output 30
But js cannot be defined like this. If you write function simue(a=1,b=2){}, it will prompt that the object is missing.
There is an array arguments that store parameters in the js function. All parameters obtained by the function will be saved to this array one by one by the compiler. Therefore, our js version supports the default value of the parameters, which can be implemented through another workaround, modifying the above example:
The code copy is as follows:
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 30