Default value settings for function parameters prior to ES6
When the current operation value is false, the latter value is always returned. For a named parameter of a function, when the value of the previous operand is false,
Will always return the latter value, for the function's named argument, if the pass value is not displayed, its value defaults to undefined, but this method is defective
When we pass in 0 o'clock for timeout, even if the value is legal, the Boolean is still false.
In this case, it is more secure to use the TypeOf
ES6 function Parameter Default value
In this function, only the first parameter is considered to be the total value to be passed in, the other two parameters have default values, and do not need to add any code that does not have the checksum value missing
MakeRequest ("/foo")//using default values for parameter timeout and callback
MakeRequest ("/foo", 500); Using default values for parameter callback
MakeRequest ("/foo", $, function (body) {dosomething (body)}); Do not use default values
By ES6, a URL is a required parameter, and when you declare a function, you can specify a default value for any parameter, and you can continue to declare no default parameter after you have specified a parameter for the default value, for example:
In this case, the default value of timeout is used only if the value is not passed in for the second argument or if it is actively passed in to the second argument undefined.
MakeRequest ("/foo", Undefined, function (body) {dosomething (body);}); Use the timeout default value
MakeRequest ("/foo"); Use the timeout default value
MakeRequest ("/foo", NULL, function (body) {dosomething (body);}); Do not use the timeout default value
For default parameter values, NULL is also a valid value
Effect of default parameter values on arguments objects
(1) ES5 non-strict mode
In non-strict mode, changes to named parameters are updated synchronously to the arguments object
(2) ES5 Strict mode
Regardless of how the named parameter changes, the arguments object will no longer change, always equal to the parameter value passed in when the function is called (i.e. ["a", "B"])
(3) in ES6
ES6, if a function uses a default parameter value, the behavior of the arguments object will be consistent with ES5 strict mode, regardless of whether the display defines strict mode.
The existence of default parameter values keeps arguments objects separate from named arguments
Only one parameter is passed to the function, then the arguments[0] value is "a", the arguments[1] value is undefined,arguments.length 1; changing first and second does not affect arguments.
So it's always possible to restore the parameters to the initial value through the arguments object
Default parameter expression
In the preceding code, if you do not pass in the last parameter, the GetValue () function is called to get the correct default value
Note: The initial parse function does not call the GetValue () method and is called when the Add () method is called and does not pass in the second argument.
When using the function call result as the default value, if you forget to write the parentheses, the incoming will be a reference to the function, not the function call result
Because the default parameter is evaluated at function invocation, you can use the first defined parameter as the default value for the post-defined parameter
You can also pass the value of first to a function to get the value of second
When referencing the default value of a parameter, only the value of the preceding parameter is allowed, that is, the parameter defined before the argument cannot be accessed.
Temporary Dead zone for default parameters
Recalling the above example, when you call add, the equivalent of executing the following code:
let first = 1;
let second = 1;
When the Add (1) is called,
let first = 1;
Let second = GetValue (first); First has been freed from the temporary Dead zone.
The above code:
When you call Add (),
let first = 1;
let second = 1;
When the Add (1) is called,
Let first = second; Error because second is in a temporary dead zone at this time
let second = 1;
function parameters have their own scope and temporary dead zone, and the scope of the function body is independent, that is, the function parameter default value cannot access the variables declared in the function body
Default values for function parameters