📄 day4_13.html
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312-80">
<style type="text/css">
<!--
a:link { color: blue; text-decoration: none}
a:visited { color: purple; text-decoration: none}
a:hover { color: #CC0033; text-decoration: underline}
-->
</style>
<title>JavaScript教程</title>
</head>
<body topmargin="1" leftmargin="2">
<table border="0" width="591" cellspacing="0">
<tr>
<td bgcolor="#ffff99" width="451">JavaScript教程 - 第四课</td>
</tr>
<tr>
<td bgcolor="#FF6600" width="451"><a href="mailto:thau@wired.com">Thau</a></td>
</tr>
</table>
<div align="left">
<table border="0" width="630" cellspacing="0">
<tr>
<td width="458" valign="top" align="left"><strong><small><small><br>
</small></small><font face="宋体" size="3">第十三页:</font><font SIZE="3">参数及返回值</font></strong><nobr>
<p ALIGN="JUSTIFY"><font face="宋体" size="3">尽管无参数的函数在减少写源码工作量,HTML源码可读性上很<br>
有用,但有参数的函数会更为有用。</font></p>
<font SIZE="3"><p ALIGN="JUSTIFY">上一例中,当返回的分、秒值小于<font
face="宋体"><big>10</big></font>时会有问题发生。我们想<br>
要看到的秒值是<font face="宋体"><big>04</big></font>而非<font face="宋体"><big>4</big></font>。我们可以这样做:</p>
</font></nobr><p ALIGN="JUSTIFY"><font face="宋体"><br>
var the_minute = the_date.getMinutes();<br>
<br>
if (the_minute < 10) <br>
<br>
{<br>
<br>
the_minute = "0" + the_minute;<br>
<br>
}<br>
<br>
<br>
<br>
var the_second = the_date.getSeconds();<br>
<br>
if (the_second < 10)<br>
<br>
{<br>
<br>
the_second = "0" + the_second;<br>
<br>
}<br>
<br>
</font><nobr><font SIZE="3">它会非常有效。但是注意,同样的源码你写了两次:若某件<br>
东西小于<font face="宋体">10</font></font><font size="2">,</font><font SIZE="3">则前面加“<font
face="宋体">0</font>”。所以要考虑当用同一代码要多<br>
次重写时,用函数来做。本例中我写了一个叫</font><font face="宋体"
size="3">fixNumber</font><font SIZE="3">的函数:</font></nobr></p>
<p ALIGN="JUSTIFY"><font face="宋体"><br>
function fixNumber(the_number)<br>
<br>
{<br>
<br>
if (the_number < 10)<br>
<br>
{<br>
<br>
the_number = "0" + the_number;<br>
<br>
}<br>
<br>
return the_number;<br>
<br>
}<br>
<br>
</font><font face="宋体" size="3"><tt>fixNumber</tt>的参数是<tt>the_number</tt>。一个参数也是一个变量,当该<br>
函数被调用时,其参数值也被设置。在本例中,我们这样调用<br>
函数: </font></p>
<p ALIGN="JUSTIFY"><font face="宋体"><br>
var fixed_variable = fixNumber(4);<br>
<br>
</font><font face="宋体" size="3">参数<tt>the_number</tt>在函数中设置为<tt>4</tt>。到现在你应该对<tt>fixNumber</tt><br>
的主体有了一定的了解。它的意思是:如果变量<tt>the_number</tt>小<br>
于10,则在它的前面加一个0。这里面新的内容是<tt>return</tt>指令:<br>
返回<tt>the_number</tt>的值。在下面的情况中就会用到return指令: </font></p>
<p ALIGN="JUSTIFY"><font face="宋体"><br>
var some_variable = someFunction();<br>
<br>
</font><font face="宋体" size="3">变量<tt>some_variable</tt>的值是<tt>函数someFunction()</tt>
的返回值。<br>
在<tt>fixNumber</tt>中,我加入: return <tt>the_number</tt>,则退出函数并<br>
将返回<tt>the_number</tt>的值返回给任何一个等待被设置的变量。<br>
所以,我这样书写代码:</font></p>
<p ALIGN="JUSTIFY"><font face="宋体">var fixed_variable = fixNumber(4);<br>
</font></p>
<p><font SIZE="3"><font face="宋体"><tt>the_number</tt></font>的初始值将通过函数调用被设置为<font
face="宋体">4</font>,然后由于<font face="宋体">4</font>小<br>
于<font face="宋体">10</font>,所以<font face="宋体"><tt>the_number</tt></font>将被改为<font
face="宋体">"<tt>04</tt>"</font>。然后<font face="宋体"><tt>the_number</tt></font>值被<br>
返回,而且变量<font face="宋体"><tt>fixed_variable</tt></font>将被设置为<font
face="宋体">"04" </font>。</font></p>
<p><font face="宋体" size="3">为了将<tt>fixNumber</tt>包括在原始函数<tt>announceTime()</tt>中,我添加了<br>
如下内容:</font></p>
<p><font face="宋体">function announceTime()<br>
<br>
{<br>
<br>
//get the date, the hour, minutes, and seconds<br>
<br>
var the_date = new Date();<br>
<br>
var the_hour = the_date.getHours();<br>
<br>
var the_minute = the_date.getMinutes();<br>
<br>
var fixed_minute = fixNumber(the_minute);<br>
<br>
var the_second = the_date.getSeconds();<br>
<br>
var fixed_second = fixNumber(the_second);<br>
<br>
<br>
//put together the string and alert with it<br>
<br>
var the_time = the_hour + ":" + fixed_minute + ":" + fixed_second;<br>
<br>
alert("The time is now: " +the_time);<br>
<br>
} <br>
<br>
</font><font face="宋体" size="3">假定时间链接被点击时,时间为12:04:05。用<tt>new
Date()</tt>获得<br>
日期,用<tt>getHours()</tt>获得小时,用前面所属方法获得分钟,分<br>
钟在本例中应该是<tt>4</tt>, 然后调用<tt>fixNumber</tt>,其参数为<tt>the_minute</tt>:</font></p>
<p><font face="宋体"><br>
var fixed_minute = fixNumber(the_minute);<br>
<br>
</font></p>
<p><font SIZE="3">当<font face="宋体"><tt>fixNumber()</tt></font>被调用时,参数<font
face="宋体"><tt>the_number</tt></font>被设置为<font face="宋体"><tt>the_minute</tt></font>。<br>
在本例中由于<font face="宋体"><tt>the_minute</tt></font>是<font face="宋体">4</font>,所以<font
face="宋体"><tt>the_number</tt></font>将被设置为<font face="宋体">4</font>。<br>
设置完参数后,我们进入函数主体。由于<font face="宋体">4</font>小于<font
face="宋体">10</font>,<font face="宋体"><tt>the_number</tt></font><br>
被改变为<font face="宋体">"<tt>04</tt>"</font>,然后<font face="宋体"><tt>the_number</tt></font>值用<font
face="宋体">return</font>指令返回。当<font face="宋体">"04"</font><br>
被<font face="宋体"><tt>fixNumber</tt></font>返回后,本例<tt><font face="宋体">fixed_minute</font></tt>就等于<font
face="宋体">"04"</font>。<font face="宋体"> </font></font></p>
<font SIZE="3"><p>我们一步一步来研究该过程。假定时间为<font
face="宋体">12:04:05</font>。 </font><ol>
<font SIZE="3"><h2>我们从函数<tt><font face="宋体">announceTime()</font>开始</tt></h2>
<li><font face="宋体"><tt>the_minute</tt> = <tt>the_date.getMinutes();</tt></font>则<font
face="宋体"><tt>the_minute</tt> = 4 </font></li>
<li><tt><font face="宋体">fixed_minute = fixNumber(the_minute);</font></tt>等于<tt>函<br>
数<font face="宋体">fixNumber()</font></tt>并将其值返回给<font face="宋体"><tt>fixed_minute</tt>
</font><h2>现在进入函数<font face="宋体"><tt>fixNumber()</tt></font></h2>
</li>
<li><tt>函数<font face="宋体">fixNumber(the_number)fixNumber()</font></tt>用<font
face="宋体"><tt>the_minute</tt></font>的<br>
值调用,<font face="宋体">the_minute</font>值是<font face="宋体">4</font>,所以现在<font
face="宋体"><tt>the_number = 4</tt> </font></li>
<li><tt>如果<font face="宋体">(the_number < 10) {the_number = "0" +<br>
the_number;}</font></tt>由于<font face="宋体">4</font>小于<font face="宋体">10</font>,所以<font
face="宋体"><tt>the_number</tt></font>现在等于<font face="宋体">"04"</font></li>
<li>返回<font face="宋体"><tt>the_number</tt></font>值<font face="宋体">,</font>退出该函数并返回值<font
face="宋体">"04" </font><p><tt>现在已经退出函数<font face="宋体">fixTime()</font></tt>,所以现在我们回<br>
到<font face="宋体"><tt>announceTime()</tt></font></p>
</li>
<li>该函数返回值为<font face="宋体">"04"</font>,所以<font
face="宋体"><tt>fixed_minute</tt> </font>现在等于<font face="宋体">"04" </font><p>该例用了一个只有一个参数的函数。实际上你可以为函数<br>
设置多个参数。例如<a href="day4_14.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_14.html">该函数输入两个数组并返回一个元素<br>
列表</a>。</p>
</li>
</font>
</ol>
<p align="left"><font face="宋体" size="3" color="#000000"><strong>JavaScript教程</strong></font><font
color="#FF0000" face="宋体" size="3"><br>
</font><font size="3" color="#FF0000">第一页</font><font size="2" face="宋体"> </font><a
href="day4_13.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_13.html"><font SIZE="3">第四课介绍</font></a><font size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第二页</font> <a href="day4_2.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_2.html">循环介绍</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第三页</font> <a href="day4_3.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_3.html">循环的密码</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第四页</font> <a href="day4_4.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_4.html">再谈<font
face="宋体">WHILE</font>循环</a></font><font size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第五页</font> <a href="day4_5.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_5.html">For循环</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第六页</font> <a href="day4_6.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_6.html">嵌套循环</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第七页</font> <a href="day4_7.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_7.html">循环练习</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第八页</font> <a href="day4_8.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_8.html">数组</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第九页</font> <a href="day4_9.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_9.html">数组和循环</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第十页</font> <a href="day4_10.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_10.html">文件目标模块中的数组</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第十一页</font> <a href="day4_11.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_11.html">函数</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第十二页</font> <a href="day4_12.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_12.html">无参数函数</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第十三页</font> 参数及返回值</font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第十四页</font> <a href="day4_14.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_14.html">多于一个参数的函数</a></font></p>
<p align="left"><font face="宋体" size="3">[<a href="index.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/index.html">第1课</a>][<a
href="day2_1.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day2_1.html">第2课</a>][<a href="day3_1.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day3_1.html">第3课</a>][第4课][<a
href="day5_1.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day5_1.html">第5课</a>]</font></p>
<hr align="left">
<p><font size="2">本文由<a href="javascript:if(confirm('http://chd.126.com/ \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://chd.126.com/'" tppabs="http://chd.126.com/" target="_blank">《</font><font
face="verdana, arial, geneva, sans-serif" size="2">CHD</font><font size="2">的网络教室》</a>根据<a
href="javascript:if(confirm('http://www.webmonkey.com.cn/ \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.webmonkey.com.cn/'" tppabs="http://www.webmonkey.com.cn/" target="_blank">《网猴》</a>相关文章改编,版权归<a
href="javascript:if(confirm('http://www.webmonkey.com.cn/ \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.webmonkey.com.cn/'" tppabs="http://www.webmonkey.com.cn/" target="_blank">《网猴》</a>所有</font></td>
</tr>
</table>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -