If the defined parameter is missing during the call of the function, the value of this parameter variable will be undefined. How to assign default values to missing parameters? Before ES6, there was no concise syntax to set the default values of missing parameters, but we can generally write code like this to solve the default values of missing parameters:
function myFunction(x, y, z) { x = x === undefined ? 1 : x; y = y === undefined ? 2 : y; z = z === undefined ? 3 : z; (x, y, z); //Output "6 7 3" } myFunction(6, 7);
Isn't this writing more troublesome? Is the default setting not intuitive enough? In ES6, we can implement it in a more concise syntax. Through this article you will learn the following:
- Declare parameters with ES6 syntax
- Values are passed using undefined
- Parameter operation
Declare parameter default values using ES6 syntax
We can use ES6's concise syntax, as shown in the following example to declare the default value of function parameters:
function myFunction(x = 1, y = 2, z = 3) { (x, y, z); } myFunction(6,7); // Outputs 6 7 3
In the above code example, we pass the first two parameters in calling this function, so the default values of the parameters (i.e. x=1 and y=2) will be overwritten (i.e. x=6 and y=7). Since the third parameter is default, z uses the default value, which will be 3.
Pass parameters using undefined
If we want a specific parameter to use the default value, we can use undefined to assign values, as shown in the following code:
function myFunction(x = 1, y = 2, z = 3) { (x, y, z); // Outputs "1 7 9" } myFunction(undefined,7,9);
Isn't it very simple? Just use undefined and we can specify the default value for a specific parameter.
Parameter operation
In ES6, we can not only assign specific values to the parameters by default, but also support the logical operations of the parameters for assignment, as shown in the following code:
function myFunction(x = 1, y = 2, z = x + y) { (x, y, z); // Output "6 7 13" } myFunction(6,7);
In the above code, we only pass in the first two parameters, the third parameter defaults, and the default value of the third parameter will be the sum of the first two parameters.
Section
That’s all for today’s content. When we develop some general components of engineering applications, we need to expose some interfaces for use. The friendliness of these interfaces is a major indicator that determines the quality of this component. The perfection of the default value of the parameter (default value) also affects the friendliness of the interface. Thanks to ES6, we can set the default value of the parameter using such a brief syntax.
Used in conjunction with deconstruction assignment default values
Here are two ways of writing that need to be distinguished:
function m1({x=0,y=0} = {}){ return [x,y]; } function m2({x,y} = {x:0,y:0}){ return [x,y]; } m1({x:3});//[3,0] m2({x:3});//[3,undefined] m1({});//[0,0] m2({});//[undefined,undefined]
The default value position of the parameter
Normally, the parameter that defines the default value should be the tail parameter of the function. Because this is easier to see which parameters are omitted? If the default value of non-tailed parameters is set, this parameter cannot be omitted.
If the default parameters are not tail parameters, then you cannot just omit the parameter without omitting the subsequent parameters unless the input is undefined. If undefined is passed in, the default value will be triggered, but null does not have this effect.
length property of function
If the function specifies the default value, the function's length property will not contain the default value parameters. This is because the meaning of the length attribute is that the number of parameters passed in this function is expected to be passed in. After a certain parameter specifies the default value, the number of parameters expected to be passed in does not include this parameter. Similarly, the rest parameter will not be included in the length attribute.
Types of default value of function parameters
(1) Variable
If the default value of a function parameter is a variable, the scope of the variable is in the same scope rules as the scope of other variables, that is, the scope of the previous function, and then the global scope.
(2) Function
If the default value of function A's parameter is function B, then since the scope of function is the scope where it is declared, the scope of function B is in the global scope rather than the scope of function A.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.