Example of passing JS Parameters

Source: Internet
Author: User

Normally, the passing parameters are written like this:
Copy codeThe Code is as follows:
Var numParameter = 123;
Function sendParameter ()
{
GetParameter (numParameter );
}
Function getParameter (sendNum)
{
Alert (sendNum );
}

Method A calls Method B and transmits parameters.

However, if the parameters of Method B are not fixed, different parameters may be required according to different situations. You can write the following:
Copy codeThe Code is as follows:
Var TXT parameter1 = "123 ";
Var txtParameter2 = "234 ";
Var txtParameter3 = "345 ";
Var txtSendParameter = "";
Var flag = 0;
Function sendParameter1 ()
{
Flag = 1;
TxtSendParameter = txtParameter1 + ";" + txtParameter2;
GetParameter ();
}
Function sendParameter2 ()
{
Flag = 2;
TxtSendParameter = txtParameter2 + ";" + txtParameter3;
GetParameter ();
}
Function getParameter ()
{
If (1 = flag)
{
// Goto functionC (txtSendParameter)
}
Else if (2 = flag)
{
// Goto functionD (txtSendParameter)
}
Else
{
Return;
}
}

You can pass the value without using global variables:
Copy codeThe Code is as follows:
Var TXT parameter1 = "123 ";
Var txtParameter2 = "234 ";
Var txtParameter3 = "345 ";
Function sendParameter1 ()
{
Var txtSendParameter = "1" + txtParameter1 + ";" + txtParameter2;
GetParameter (txtSendParameter );
}
Function sendParameter2 ()
{
Var txtSendParameter = "2" + txtParameter2 + ";" + txtParameter3;
GetParameter (txtSendParameter );
}
Function getParameter ()
{
Switch (arguments [0])
{
Case "1 ":
// Goto functionC (arguments );
Break;
Case "2 ":
// Goto functionD (arguments );
Break;
Default:
// Goto functionE (arguments );
}
}

Relatively speaking, arguments is a flexible object. Although it is not an array, it can use subscript values like array. Although it is a bit uncommon, it is still very useful.
Copy codeThe Code is as follows:
<Pre code_snippet_id = "179244" snippet_file_name = "external" name = "code" class = "javascript"> <pre code_snippet_id = "179244" snippet_file_name = "external"> </pre>
<Pre> </pre>
<Pre> </pre>
<Pre> </pre>
<Pre> </pre>
<Pre> </pre>

</Pre>

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.