A work und for setting default values for js function parameters _ javascript tips-js tutorial

Source: Internet
Author: User
The js function has an array arguments that stores parameters, therefore, functions supporting default parameter values in js can be implemented in php using another work ing method. It is convenient to set the default value for parameters when defining a function, for example:

The Code is as follows:


Function simue ($ a = 1, $ B = 2 ){
Return $ a + $ B;
}
Echo simue (); // output 3
Echo simue (10); // OUTPUT 12
Echo simue (10, 20); // output 30


But JavaScript cannot be defined as this. If function simue (a = 1, B = 2) {} is written, a message indicating a missing object is displayed.

The js function contains an array arguments for storing parameters. All parameters obtained by the function are saved to this array by the compiler one by one. Therefore, the js version supports the default parameter values. You can use another work und to modify the preceding example:

The Code is as follows:


Function simue (){
Var a = arguments [0]? Arguments [0]: 1;
Var B = arguments [1]? Arguments [1]: 2;
Return a + B;
}
Alert (simue (); // output 3
Alert (simue (10); // OUTPUT 12
Alert (simue (10, 20); // output 30

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.