The method of specifying the default value of multiple parameters of JS function _javascript tips

Source: Internet
Author: User

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):

function Mfun (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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.