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.