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.