Examples of functions with variable JavaScript parameters: javascript variable
Digress: I have been familiar with JavaScript For a long time, but I did not pay attention to it. I have seen JavaScript on many cool and dazzling web pages. Google's JavaScript Application has the biggest impact on me. I decided to learn it from the beginning, so I had the JavaScript & Ajax column. I plan to record this column as a study note, so every article note may be short, just one or two notes.
JavaScript allows a function to pass a variable number of parameters. Because arguments is a built-in object, it is an array of all parameters passed by a function. For example, you will understand.
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Functions with variable JavaScript parameters </title>
<Mce: script language = "javascript" type = "text/javascript"> <! --
Function testparams ()
{
Var params = "";
For (var I = 0; I <arguments. length; I ++ ){
Params = params + "" + arguments [I];
}
Alert (params );
}
Testparams ("abc", 123 );
Testparams (123, "456", 789 );
Testparams ();
// --> </Mce: script>
</Head>
<Body>
</Body>
</Html>
Javascript function as parameter name
A simple example. Test environment: win7 + ie8 + chrome
<Script type = "text/javascript">
Function Add (x, y ){
Alert (x + y );
}
Function Sub (x, y ){
Alert (x-y );
}
Function CallFunc (Fun, x, y ){
If (Fun & (typeof Fun = "function ")){
If (x & y & (typeof x = "number") & (typeof y = "number ")){
Fun (x, y );
} Else {
Console. log (typeof x );
}
} Else {
}
}
CallFunc (Sub, 5, 3 );
</Script>
Definition of the number of parameters with the same name in a javascript function
In this way, you can write a few parameters at will.
Function myFunction ()
{
// Do something
Alert (myFunction. length );
For (var I = 0; I <myFunction. length; I ++)
{
Alert (myFunction. arguments [I]);
}
}
MyFunction (1 );
MyFunction (1, 2 );
MyFunction (1, 2, 3 );