Example of setting default parameter values in Javascript and javascript

Source: Internet
Author: User

Example of setting default parameter values in Javascript and javascript

First:

Function test (a, B) {var a = arguments [0]? Arguments [0]: 1; // set the default value of parameter a to 1var B = arguments [1]? Arguments [1]: 9; // set the default value of parameter B to 9 return a + B;

It is equivalent

Function test () {var a = arguments [0]? Arguments [0]: 1; // set the default value of parameter a to 1var B = arguments [1]? Arguments [1]: 9; // set the default value of parameter B to 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

Second:

Function test (blog, address) {blog = blog | 'forgot ~ Ash'; address = address | 'www .jb51.net'; alert (the address of 'blog name is '+ blog +' is '+ address );}

It is equivalent

Function test (blog, address) {if (! Blog) {blog = 'forgot ~ Ash';} if (! Address) {address = 'www .jb51.net';} alert (the 'blog name is '+ blog +' and the address is '+ address );}

Call example

Test (); // The blog name is forgotten ~ The source address is www. jb51.nettest ('csdn ', 'blog .csdn.net'); // If the blog name is csdn, the address is blog. csdn. nettest ('', 'blog .csdn.net/u011043843'); // The blog name is forgotten ~ Simple thinking

Third:

Function test (setting) {var defaultSetting = {name: 'programmer ', age: '1', phone: '000000', QQ: '000000', message: 'Welcome to add'}; $. 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: programmer, age: 1, tel: 15602277510, QQ group: 259280570, note: welcome to join. Test ({name: 'dwqs ', age: '20', QQ: '000000', message: 'blog: www.jb51.net'}); // output: name: dwqs, age: 20, tel: 15602277510, QQ group: 461147874, note: Blog: www.jb51.net.

Ps: This method can be used if there are many function parameters. This is an extension of JQuery, so JQuery needs to be introduced.


In javascript, when a function has multiple parameters, how does one "enter only" specific parameters?

In the order of functions, the values before pay can be specified as null (if there is internal verification, enter the default value without errors), and the values after pay can be omitted.

For example:

Var Test = function (arg1, arg2, pay, arg3, arg4 ){...}

Can be called: Test (null, null, 'pay ');
 
Setting multiple parameters in javascript Functions

OnClick = "biaoji ('<% = xzuo %>', '<% = yzuo %>', '<% = zzuo %> ');"

Replace it with this.

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.