Examples of functions with variable JavaScript parameters: javascript variable

Source: Internet
Author: User

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 );

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.