Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to set the default parameters of js function
How to set the default parameters of js function
1.php has a very convenient usage. When defining a function, you can directly set the default values of parameters, such as:

Function simulation ($a= 1, $b=2){

Return $ a+$ b;;

}

echo simue(); //Output 3

echo simue( 10); //output 12

echo simue( 10,20); //Output 30

However, js cannot be defined in this way. If you write the function Simule (a = 1, b = 2) {}, you will be prompted that the object is missing.

2.2.js function has a parameter array, and all the parameters obtained by the function will be saved in this array by the compiler one by one. Therefore, the js version of the function that supports the default value of parameters can be realized by another alternative method. The above example is modified:

Function simue (){

Var a = parameter [0]? Parameter [0]:

1;

Var b = parameter [1]? Parameter [1]:

2;

Return a+b;

}

alert(simue()); //Output 3

alert(simue( 10)); //output 12

alert( simue( 10,20)); //Output 30