Examples of how to set the default value _ Basics for JavaScript method parameters

Source: Internet
Author: User
Tags extend ming

Have you ever been in a situation where you wrote a function with no parameters.

function Showuserinfo () {
alert ("Hello!)" I am Xiao Ming. ");
}

function Showuserinfo () { 
  alert ("Hello!)" I am Xiao Ming. "); 
} 

Call:

Showuserinfo ();

Showuserinfo (); 

Later, it was found that other places also needed this function, but the variable value has been written dead in the function, what to do? Let's add a parameter.

function Showuserinfo (name) {
name=name| | " Xiaoming ";
Alert ("Hello!") I'm a "+name+". ");
}

function Showuserinfo (name) { 
  name=name| | " Xiaoming "; 
  Alert ("Hello!") I'm a "+name+". "); 
} 

Explain: name=name| | " Xiaoming "This code means that if name is null, it is equal to the default value" Xiaoming ". You can also write this:

function Showuserinfo (name) {
//name=name| | " Xiaoming ";
if (!name) {
name= "xiaoming";
}
Alert ("Hello!") I'm a "+name+". ");
}

function Showuserinfo (name) { 
//name=name| | " Xiaoming "; 
  if (!name) { 
    name= "xiaoming"; 
  } 
  Alert ("Hello!") I'm a "+name+". "); 
} 


Call:

Showuserinfo (' Xiao Li ');

Showuserinfo (' Xiao Li '); 


Later, the demand changed again, need to add age. All right, then change:

function Showuserinfo (name,age) {
name=name| | " Xiaoming ";
age=age| | ;
Alert ("Hello!") I am "+name+", this year "+age+" years old. ");
}

function Showuserinfo (name,age) { 
  name=name| | " Xiaoming "; 
  age=age| | ; 
  Alert ("Hello!") I am "+name+", this year "+age+" years old. "); 
} 

Call:

Showuserinfo (' Xiao Li ');/result: Hello! I am Xiao Li, 22 years old this year.
showuserinfo (' Xiao Li ', 19);/Result: Hello! I am Xiao Li, 19 years old this year.
Showuserinfo (null,19)//Result: Hello! I am Xiao Ming, 19 years old this year.

showuserinfo (' Xiao Li ');/result: Hello! I am Xiao Li, 22 years old this year. 
showuserinfo (' Xiao Li ', 19);/Result: Hello! I am Xiao Li, 19 years old this year. 
Showuserinfo (null,19)//Result: Hello! I am Xiao Ming, 19 years old this year. 


OK, if we need to add a birthday, telephone, sex, QQ and so on parameters, what should we do? Do you want to set a default value for one? In fact, I've seen a lot of people do that. Here's a simpler way to look at it. That's the extension using jquery. First look at the code:

 function Showuserinfo (setting) {var defaultsetting={name: "Xiaoming", Age: "All", Sex: "Male", Phone:
"13888888888", QQ: "12345678", Birthday: "1980.12.29"};
$.extend (defaultsetting,settings); var message= ' name: ' +defaultsetting.name + ', sex: ' +defaultsetting.sex + ', Age: ' +defaultsetting.age + ', tel: ' + Defaultsetting.phone + ', QQ: ' +defaultsetting.qq + ', Birthday: ' +defaultsetting.birthday + '.
';
alert (message); function Showuserinfo (setting) {var defaultsetting={name: "Xiaoming", Age: "", Sex: "Male", Phone: "13888 
   
  888888 ", QQ:" 12345678 ", Birthday:" 1980.12.29 "}; 
   
  $.extend (defaultsetting,settings); var message= ' name: ' +defaultsetting.name + ', sex: ' +defaultsetting.sex + ', Age: ' +defaultsetting.age + ', tel: ' +d Efaultsetting.phone + ', QQ: ' +defaultsetting.qq + ', Birthday: ' +defaultsetting.birthday + '. 
  '; 
alert (message); } 

Description: The role of $.extend (defaultsetting,settings) is to merge the incoming setting configuration with defaultsetting and overwrite the setting configuration with the configuration in defaultsetting.

Call:

Showuserinfo ({
name: "Xiao Li"
});
Results: Name: Xiao Li, Sex: Male, Age: 22, Tel: 13888888888,qq:12345678, Birthday: 1980.12.29.
Showuserinfo ({
name: "Little Red",
Sex: "Female",
Phone: "13777777777"
});
Results: Name: Xiao Hong, sex: female, Age: 22, Tel: 13777777777,qq:12345678, Birthday: 1980.12.29.




Showuserinfo ({ 
  name: "Xiao Li" 
}); 
Results: Name: Xiao Li, Sex: Male, Age: 22, Tel: 13888888888,qq:12345678, Birthday: 1980.12.29. 
Showuserinfo ({ 
  name: "Little Red", 
  Sex: "Female", 
  Phone: "13777777777" 
}); 
Results: Name: Xiao Hong, sex: female, Age: 22, Tel: 13777777777,qq:12345678, Birthday: 1980.12.29. 


It's simple! In this way, even if there are 100 parameters, are not afraid.

So when do you use multiple parameters and when do you use such a Parameter object? My experience is, according to the actual need, if use one or two parameters can be done without the use of parameter objects. More than 3, I'll use this parameter object.

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.