JavaScript Tutorial/Function/Function Parameters
Версия от 18:52, 25 мая 2010; (обсуждение)
Содержание
Pass an array to a function
<HTML>
<HEAD>
<TITLE>
Iteration Two
</TITLE>
<SCRIPT>
function makeArray() {
var myArray = new Array(4);
myArray[0] = "A";
myArray[1] = "B";
myArray[2] = "C";
myArray[3] = "D";
return myArray;
}
function showArray(theArray){
var quote = "";
for (var i = 0; i < theArray.length; i++){
quote += theArray[i] + " ";
}
return quote;
}
</SCRIPT>
</HEAD>
<BODY>
<H1>
<SCRIPT>
var x = makeArray();
document.write(showArray(x));
</SCRIPT>
</H1>
</BODY>
</HTML>
Pass Form value to a function
<HTML>
<HEAD>
<TITLE>
Add three numbers
</TITLE>
<SCRIPT>
function addThreeNums (inOne, inTwo, inThree) {
var inOne = Number(inOne);
var inTwo = Number(inTwo);
var inThree = Number(inThree);
return Number(inOne + inTwo + inThree);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM Name="theForm">
<INPUT Type=Text Name="num1">
<INPUT Type=Text Name="num2">
<INPUT Type=Text Name="num3">
<INPUT Type=Button Value="Add Them"
onClick="document.write("sum:" +addThreeNums(theForm.num1.value,theForm.num2.value,theForm.num3.value));">
</FORM>
</BODY>
</HTML>
Pass integer to function
<html>
<head>
<script language="JavaScript" type = "text/javascript">
<!--
function DisplayMsg(NumVal) {
if (NumVal == 1) {
status = "Type your name in the field" ;
}
if (NumVal == 2) {
status = "Type your phone number in the field"
}
}
//-->
</script>
<title>Keyboard Event</title>
</head>
<body>
<form name="form1">
<b>Name:</b> <input type = "text" name = "text1" onFocus="DisplayMsg(1)" size="20"><P>
<b>Phone:</b> <input type = "text" name = "text2" onFocus="DisplayMsg(2)" size="20"></p>
</form>
</body>
</html>
Pass number to a function
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
function calcAvg()
{
var inpNum1 = 1;
var inpNum2 = 2;
var inpNum3 = 3;
var inpNum4 = 4;
numAvg = doCalcAvg(inpNum1, inpNum2, inpNum3, inpNum4);
document.write("The average of the four numbers you entered is: " + numAvg);
}
function doCalcAvg(inpNum1, inpNum2, inpNum3, inpNum4)
{
var ans;
ans = (Number(inpNum1) + Number(inpNum2) + Number(inpNum3) + Number(inpNum4)) / 4;
return (ans);
}
// -->
</script>
</head>
<body onLoad="calcAvg();">
</body>
</html>
Pass value to a function
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
function yourMessage(quote)
{
alert(quote);
}
// -->
</script>
</head>
<body>
<P>Click <input type="button" value="AAA" onClick="yourMessage("AAA")"> for a message!</p>
<br>
<P>Click <input type="button" value="BBB" onClick="yourMessage("BBB")"> for another message!</p>
</body>
</html>
Use functionName.arguments to reference the arguments
<HTML>
<HEAD>
<TITLE>
Add all of the numbers
</TITLE>
<SCRIPT>
function addNums () {
var theAnswer = 0;
for (var i = 0; i < addNums.arguments.length; i++) {
var theNum = Number(addNums.arguments[i]);
theAnswer += theNum;
}
return theAnswer;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM Name="theForm">
<INPUT Type=Text Name="num1">
<INPUT Type=Text Name="num2">
<INPUT Type=Text Name="num3">
<INPUT Type=Text Name="num4">
<INPUT Type=Button
Value="Add Them"
onClick="document.write(addNums(theForm.num1.value,theForm.num2.value,theForm.num3.value,theForm.num4.value));">
</FORM>
</BODY>
</HTML>