Example of setting default parameter values in JavaScript

Source: Internet
Author: User

First type:

function test (a,b) {

var a = Arguments[0]? Arguments[0]: 1;//The default value of setting parameter A is 1

var B = arguments[1]? ARGUMENTS[1]: 9;//The default value of setting parameter B is 9

return a+b;

It is equivalent to

function Test () {

var a = Arguments[0]? Arguments[0]: 1;//The default value of setting parameter A is 1

var B = arguments[1]? ARGUMENTS[1]: 9;//The default value of setting parameter B is 9

return a+b;

}

Call Example

Alert (test ()); Output 10

Alert (Test (5)); Output 14

Alert (Test (5,6)); Output 11

Alert (Test (null,6)); Output 7

Alert (Test (6,null)); Output 15

The second type:

function test (blog,address) {

blog=blog| | ' Forgotten ~ shallow thinking ';

address=address| | ' Www.jb51.net ';

Alert (' Blog name is ' +blog+ ' address is ' +address ');

}

It is equivalent to

function test (blog,address) {

if (!blog) {blog= ' forgotten ~ shallow thinking ';}

if (!address) {address= ' www.jb51.net ';}

Alert (' Blog name is ' +blog+ ' address is ' +address ');

}

Call Example

Test (); The name of the blog is forgotten ~ The address of the shallow thinking is www.jb51.net

Test (' csdn ', ' blog.csdn.net '); The blog name is Csdn's address is blog.csdn.net

Test (', ' blog.csdn.net/u011043843 '); Blog name is forgotten ~ Shallow thinking

The third type:

function test (setting) {

var defaultsetting={

Name: ' Program Enthusiast ',

Age: ' 1 ',

Phone: ' 15602277510 ',

QQ: ' 259280570 ',

Message: ' Welcome to your join '

};

$.extend (defaultsetting,setting);

var msg= ' name: ' +defaultsetting.name

+ ', Age: ' +defaultsetting.age

+ ', Tel: ' +defaultsetting.phone

+ ', QQ Group: ' +defaultsetting.qq

+ ', Description: ' +defaultsetting.message

+ '. ';

Alert (msg);

}

Call Example

Test (); Output: Name: Program enthusiasts, Age: 1, Tel: 15602277510,qq Group: 259280570, Description: welcome you to join.

Test ({

Name: ' Dwqs ',

Age: ' 20 ',

QQ: ' 461147874 ',

Message: ' Blog: www.jb51.net '

});

Output: Dwqs, Age: 20, Tel: 15602277510,qq Group: 461147874, Description: Blog: www.jb51.net.

PS: The function parameters are relatively long, you can use this method. This is an extension of jquery, so you need to introduce jquery.

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.