JavaScript functions participate in formal parameters--intelligent and principled

Source: Internet
Author: User

1. The number of actual participation parameters can be biased, not because the parameter is not uniform and error.

Example 1:

Functionbox (b) {                            return a + B;} alert (Box (1,3,5));        Run Result: "4".                           //The operation is "1+3" and does not use "5".

Example 2:

Functionbox (a,b,c) {    return a + b+c;} alert (Box (1, ' 5 '));         Operation Result: 15undefined.                            //The result is of type sring. The operation 1 is connected to the ' 5 ' character, and then to a non-existent (undefined) character, the result is 15undefined.

2.JS functions cannot be overloaded.

Because "function overloading" is the same function name according to different parameters, matching a particular function, thus executing the function of the process, and JS function no longer exclude "alien" function, missing the same function name to identify the different functions of the process, the same function name of the case, JS call, only the last function will be executed.

Example 3:

function box (a,b,c) {    alert (' Executes the first function, ' + A + ' + ' + ' + c + ' = ' + ' + (A+C))} function box (a,b,c) {    alert (' Executes the second function, ' + A + ' + ' + B + ' = ' + (a+b)); }                alert (Box (1, ' 5 ', 3));    The result of execution is: The second function is executed, 1+5=15                        

Example 4:

function box (a,b,c) {    alert (' Executes the second function, ' + A + ' + ' + ' + ' + ' + ' + ' + ' + ' + ' = ' + (a+b))} function box (a,b,c) {    alert (' Executes the first function, ' + A + ' + ' + c + ' = ' + (a+c)); }               alert (Box (1, ' 5 ', 3));    The result of execution is: The first function is executed, 1+3=4                        

3. Function using arguments object, very characteristic

The use of arguments objects is a good explanation for the principle problem of supporting formal parameters, inconsistent of arguments and not error.

3.1 Arguments[index] Specifies the value of the parameter, index of the parameter

Example 5:

function box () {    return arguments[0]+arguments[5];} Alert (Box (1,2,3,4,5,6));                       Execution results are: 7alert (Box (' Test ', ' usecase ', 3,4,5,6));          The result of execution is: Test6                                               //arguments[0] represents the first argument, Arguments[5] represents the sixth parameter, and the above two execution results reflect that the box () function only uses the specified parameters as required, and the other parameters are ignored. </span>

3.2 Arguments.length Get the number of parameters

Example 6:

function box () {    return arguments.length;} Alert (Box (1,2,3,4,5,6));                      Execution results are: 6alert (Box (' Test ', ' usecase ', 3,4,5,6));         The result is: 6                                              //When you need to know how many elements of a sequence are passed into the function, you will know </span>

Extension and application of 3.3 arguments.length

Example 7:

function box () {    var sum = 0;    for (Var i=0;i<arguments.length;i++) {       sum + = Arguments[i];    }    return sum;} Alert (Box (1,3,7,14));                         Operating Result:                                              //This way, the box () function does not have a tube parameter in the end there are several </span>

4. An obvious mistake (just to explain the problem below)

See the Example 7, may be a little confused, thus adding example 8, example 9, all illustrate a problem, the parameters for the function body to provide the necessary information for can be controlled to achieve the function. Functions (function: functions )

Example 8:

function box (A, B) {    return a+b+c;} Alert (Box (n/a));                                       REFERENCEERROR:C is not defined alert (box);                                         REFERENCEERROR:C                                                          is not defined//literal C undefined </span>

Example 9:

function box () {    return a+b+c;}                           Alert (Box (n/a));                                       REFERENCEERROR:A                                                         is not defined//literal a not defined </span>

A simple comparison Example 2, example 4, example 8, can be concludedthat the function inJS and the formal parameters, arguments and the relationship between the shape participation is:

Follow a principle: function implementation function

function body and formal parameter relationship: Formal parameters must satisfy the necessary requirements of the function body, otherwise it is impossible to do, that is, using the formal parameter undefined ' literal ', error:c is not defined.

Formal participation in the actual parameter relationship: The actual parameter in the parameters of the ruler, the more intercepted and ignored, the missing is judged to be undefined (not exist), but do not error.

Intelligent: It is said that the JS function to the more than the actual parameter refine, to the few actual parameters to do the appropriate treatment, no Reiga, so that the function of the operation of a more limited smooth coordination.

JavaScript functions participate in formal parameters--intelligent and principled

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.