How to set the default value for javascript method parameters

Source: Internet
Author: User

Have you ever encountered such a situation? You have written a function with no parameters.
Js Code
1. function showUserInfo (){
2. alert ("Hello! I'm James. ");
3 .}
Call:
Js Code
1. showUserInfo ();
 
Later, I found that this function is also needed elsewhere, but the variable value is already written to the function. What should I do? Add a parameter.
Js Code
1. function showUserInfo (name ){
2. name = name | "James ";
3. alert ("Hello! I am "+ name + ". ");
4 .}
Note: name = name | "Xiao Ming" indicates that if name is null, it is equal to the default value "Xiao Ming ". You can also write as follows:
Js Code
1. function showUserInfo (name ){
2. // name = name | "James ";
3. if (! Name ){
4. name = "James ";
5 .}
6. alert ("Hello! I am "+ name + ". ");
7 .}
 
Call:
Js Code
1. showUserInfo ('lily ');
 
Later, the demand changed and the age needed to be added. Let's try again:
Js Code
1. function showUserInfo (name, age ){
2. name = name | "James ";
3. age = age | 22;
4. alert ("Hello! I am "+ name +", this year "+ age +" years old. ");
5 .}
Call:
Js Code
1. showUserInfo ('lil'); // result: Hello! I'm Xiao Li, 22 years old.
2. showUserInfo ('lil', 19); // result: Hello! I'm Xiao Li, 19 years old.
3. showUserInfo (null, 19); // result: Hello! I'm James, 19 years old.
 
Well, what should we do if we need to add parameters such as birthday, phone number, gender, and QQ? Do I set the default values one by one? In fact, I see many people do. Let's look at a simpler method. That is, Jquery extension is used. First look at the Code:
Js Code
1. function showUserInfo (setting ){
2. var defaultSetting = {
3. name: "James ",
4. age: "22 ",
5. sex: "male ",
6. phone: "13888888888 ",
7. QQ: "12345678 ",
8. birthday: "1980.12.29"
9 .};
10.
11. $. extend (defaultSetting, settings );
12.
13. var message = 'name: '+ defaultSetting. name
14. + ', Gender:' + defaultSetting. sex
15. + ', age:' + defaultSetting. age
16. + ', tel:' + defaultSetting. phone
17. + ', QQ:' + defaultSetting. QQ
18. + ', birthday:' + defaultSetting. birthday
19. + '. ';
20. alert (message );
21 .}
Note: $. extend (settings ultsetting, settings) is used to merge the passed setting configuration with defaultSetting, and overwrite the defaultSetting configuration with the configuration in setting.
 
Call:
Js Code
1. showUserInfo ({
2. name: "Xiao Li"
3 .});
4. // result: Name: Xiao Li, Gender: male, age: 22, tel: 13888888888, QQ: 12345678, birthday: 1980.12.29.
5. showUserInfo ({
6. name: "Xiaohong ",
7. sex: "female ",
8. phone: 13777777777"
9 .});
10. // result: Name: Xiaohong, Gender: female, age: 22, tel: 13777777777, QQ: 12345678, birthday: 1980.12.29.
 
Easy! In this way, even if there are 100 parameters, you are not afraid.
 
So when will multiple parameters be used and when will such parameter objects be used? My experience is that, based on actual needs, if one or two parameters are used, the parameter object will not be used. If there are more than three objects, I will use this parameter object.

 

From the ashes of time

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.