Example of setting default parameter values in JavaScript _javascript tips

Source: Internet
Author: User

First type:

function test (a,b) {

var a = Arguments[0]? arguments[0]: 1;//Setting the default value of parameter A is 1

var b = arguments[1]? arguments[1]: 9; The default value for setting parameter B is 9 return

a+b;

It is equivalent to

function test () {

var a = Arguments[0]? arguments[0]: 1;//Setting the default value of parameter A is 1

var b = arguments[1]? arguments[1]: 9;// The default value for parameter B is 9 return

a+b;

Call Example

Alert (test ()); Output

alert (Test (5));//Output

alert (test (5,6));//Output One

alert (test (null,6));//Output 7

alert ( 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 (); Blog name is forgotten ~ The address is www.jb51.net

test (' csdn ', ' blog.csdn.net ');//Blog name is CSDN 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 you to 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: ' m

',

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.

Related Article

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.