Detailed description of argument in the basic Javascript tutorial, basic tutorial argument

Source: Internet
Author: User

Detailed description of argument in the basic Javascript tutorial, basic tutorial argument

Argument is a special parameter of a function in javascript. For example,Use argument to access function parameters and determine whether the function is executed

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function sayHello (){
If (arguments [0] = "bye ")
Return;
Else
Alert ("hello" + arguments [0]);

}
</Script>

 The length attribute of argument can be used to return the number of parameters.

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function cNumbArg (){
Return arguments. length;
}
Document. write (cNumbArg (, "hello") + "<br>"); // 3 is returned.
Document. write (cNumbArg () + "<br>"); // 0
Document. write (cNumbArg (1111) + "<br>"); // 1
</Script>

Simulate and reload functions using argument objects

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function fnAdd (){
If (arguments. length = 0)
Return;
Else if (arguments. length = 1)
Return arguments [0] + 5;
Else (arguments. length> 1)
Var iSum = 0
For (var I = 0; I <arguments. length; I ++)
ISum + = arguments [I];
Return iSum;
}
Document. write (fnAdd (5) + "<br> ");
Document. write (fnAdd (10) + "<br> ");
Document. write (fnAdd (10, 20) + "<br> ");
Document. write (fnAdd (10, 20, 30, 40) + "<br> ");
</Script>

Do you have a new understanding of argument? In fact, he can do more things, and shoes will try more.

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.