What is the default parameter value for function settings in Javascript?

Source: Internet
Author: User
Tags smarty template

For how to set the default parameter values for functions in Javascript (js), the following methods are provided for reference.

Method 1:

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

Note that the above functions can also be written as follows:

Function example () {var a = arguments [0]? Arguments [0]: 1; // set the default value of the first parameter to 1var B = arguments [1]? Arguments [1]: 2; // set the default value of the second parameter to 2 return a + B ;}

Call example:

Alert (example (); // output 3 alert (example (10); // OUTPUT 12 alert (example (10, 20 )); // output 30 alert (example (null, 20); // output 20

Method 2:

Function example (name, age) {name = name | 'mink xy'; age = age | 21; alert ('Hello! I am '+ name +', and I am '+ age +' years old. ');}

This function can also be written as follows:

Function example (name, age) {if (! Name) {name = 'mink xy';} if (! Age) {age = 21;} alert ('Hello! I am '+ name +', and I am '+ age +' years old. ');}

Call example:

Example ('wang wu'); // output: Hello! I'm Wang Wu, 21 years old. Example ('wang wu', 30); // output: Hello! I'm Wang Wu, 30 years old. Example (null, 30); // output: Hello! I'm a 30-year-old.

The third method is suitable for scenarios with many parameters. Jquery extensions are used:

Function example (setting) {var defaultSetting = {name: 'marchine', age: '30', sex: 'female ', phone: '123', QQ: '123 ', birthday: '2017. 10.01 '}; $. extend (defaultSetting, settings); var message = 'name: '+ defaultSetting. name + ', Gender:' + defaultSetting. sex + ', age:' + defaultSetting. age + ', tel:' + defaultSetting. phone + ', QQ:' + defaultSetting. QQ + ', birthday:' + defaultSetting. birthday + '. '; Alert (message );}

Call example:

Example ({name: 'wang Zhaojun ', sex: 'female', phone: '000000'}); // output: name: Wang Zhaojun, Gender: female, age: 30, tel: 10089, QQ: 10086, birthday: 1949.10.01.
Articles you may be interested in
  • Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template
  • Js modifies the value of a specified parameter in the url
  • In php, we use the curl post method to submit data and the get method to obtain webpage data.
  • Functions related to cookie operations in JavaScript
  • Javascript shares the content with the code Summary of the websites on major social networking platforms
  • Javascript determines whether the mouse is left or right-click-compatible with ie, firefox, chrome, and other major browsers
  • How to accurately count the number of online users using php
  • MySQL replace function replacement string statement usage

Related Article

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.