Home > Web Front-end > JS Tutorial > JS parameter passing example introduction_javascript skills

JS parameter passing example introduction_javascript skills

WBOY
Release: 2016-05-16 17:01:12
Original
966 people have browsed it

Normally, passing parameters is probably written like this:

Copy code The code is as follows:

var numParameter = 123;
function sendParameter()
{
getParameter(numParameter );
}
function getParameter(sendNum)
{
alert(sendNum);
}

A very simple method A calls method B and passes parameters.

However, if the parameters of method B are not fixed, different parameters may be required according to different situations. You can write like this:
Copy code The code is as follows:

var txtParameter1 = "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;
}
}

So can you do it without using global variables? Pass value:
Copy code The code is as follows:

var txtParameter1 = "123";
var txtParameter2 = "234";
var txtParameter3 = "345";
function sendParameter1()
{
var txtSendParameter = "1" txtParameter1 ";" txtParameter2;
getParameter(txtSendParameter ; )
{
switch(arguments[0])
{
case "1":
//goto functionC(arguments);
break;
case "2" :
//goto functionD(arguments);
break;
default:
//goto functionE(arguments);
}
}


Relatively speaking, arguments is a very flexible object. Although it is not an array, it can use subscripts to obtain values ​​​​like arrays. Although it is a bit unfamiliar, it is still very useful.




Copy code


The code is as follows:
 

< ;/pre>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template