The example of this article tells the JS function to specify the method of the default value of multiple parameters. Share to everyone for your reference, specific as follows:
When a function has a parameter, it was previously defined (parameter is p1):
When you need to set a default value for P1
function Mfun (p1) {
if (p1===undefined) p1=5;//The default value is set to 5
...
}
When a function requires 2 parameters, it used to be written like this
function Mfun (P1,P2) {...}
Later found that there is no need to write, the JS function does not even need to be in brackets preset parameter names, you can use a way to pass any number of parameters automatically fit, when not defined, these unassigned parameters are invoked when the value is undefined
The following example demonstrates a JS function of 2 parameters
function Mfun () {
var p1 = arguments[0]? Arguments[0]:-1;//Set parameter P1 default value is-1
var p2 = arguments[1]? arguments[1] : ' ABC '; P2 default value ' ABC ' ...
}
Here are some error demonstrations:
2 parameters are required and the second is defined as optional parameters
function Mfun (p1) {...}
function Mfun (P1,P2) {...}
* This method of writing, Mfun (p1) will be covered by the following function, when only one parameter, P2 will prompt undefined
Funciton mfun (p1,p2= ' xxx ') {...}
This is the PHP habit. =___=b..
Look at one more 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
alert (Simue (10,20));/Output 30
For more information on JavaScript-related content readers can view the site topics: "JavaScript common function Tips Summary", "JavaScript value-handling Skills Summary", "JavaScript Coding Operation Skills Summary", " A summary of JSON manipulation techniques in JavaScript, a summary of JavaScript errors and debugging techniques, a summary of JavaScript data structure and algorithm techniques, JavaScript traversal algorithms and techniques summary, and a summary of JavaScript mathematical operational usage 》
I hope this article will help you with JavaScript programming.