Tenth--------JavaScript functions-parameters

Source: Internet
Author: User

Parameters of the JavaScript function: formal parameter, actual parameter

//function Parameters                        //formal parameter list            functionTest (a,b,c,d) {//alert (test.length); Number of parameters, 4                                //The actual argument of the function, which is to use an array to receive the actual parameters of the function                //arguments object that can access the actual parameters of the function
Arguments objects can only be used inside a function
alert (arguments.length);//2 xAlert (arguments[0]);//TenAlert (arguments[1]);// - returnA +b; } alert (Test (10,20));//30, the parameters of the function and the actual parameter are inconsistent without error.

However, this program is not rigorous, we hope that only the physical parameters and the number of arguments consistent with the time to execute the function, or throw an exception or give a hint

So add judgment:

if (Test.length = = arguments.length)       {return a +b;  } Else {       return ' parameter is incorrect!} ';}

There are problems, although test.length can take the number of formal parameters, but generally not so use, there will be potential problems. General use Arguments.callee Example:

// recursive            function fact (num) {                ifreturn 1;                 Else                 return num*fact (num-1);                }             var F = fact;    // you assign the fact to the variable F, which is equivalent to copying a copy of the            null;    // Suppose someone else had made the fact null,            Alert (F (5));    Error

But using Arguments.callee instead of fact would not be an error:

//Recursive            functionfact (num) {if(num<=1)return1; Else                 returnNum*arguments.callee (num-1);//using Arguments.callee, point to the function itself            }            varF = fact;//you assign the fact to the variable F, which is equivalent to copying a copy of theFact =NULL;//Let's say someone else made the fact null.Alert (F (5));

So the upper use of the parameter is not test.length, also use arguments.callee.length:

if (Arguments.callee.length = = arguments.length)         {return a +b;} Else {        return ' parameter is incorrect!} ';}

Summarize:

Tenth--------JavaScript functions-parameters

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.