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.