📄 day1_4.html
字号:
<tr bgcolor="#FFFFFF">
<td colspan="2">-<font color="#000000"><a href="../../1-download/page2.htm">程序设计</a></font></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<font color="#000000"><a href="../../1-download/page3.htm">网站设计</a></font></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<font color="#000000"><a href="../../1-download/page4.htm">电子商务</a></font>
</td>
</tr>
<tr>
<td colspan="2" align="center" bgcolor="#666699"><a href="../../1-teach/index.htm"><font color="#FFFFFF">实用技巧</font></a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/internet/index.htm">Internet应用</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/photoshop/index.html">Photoshop</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/flash/page1.html">Flash</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/asp/index.html">ASP</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/php/index.html">PHP</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/java/index.htm">Java</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/vb/index.htm">VB</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/c/index.htm">C、C++</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-backend/database/php_mysql/index.html">PHP/MySQL</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2"><a href="../../1-backend/cgi_perl/perl_beginner/index.html">-Perl</a>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/other/index.htm">其它</a> </td>
</tr>
<tr>
<td colspan="2" bgcolor="#666699">
<div align="center"><font color="#FFFFFF">更多教程</font></div>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="17"><a href="../../1hdml/index.html">-HDML</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="23"><font face="宋体"><a href="../../1-backend/database/course/day1_1.html">-网络数据库</a></font></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="14"><a href="../../1-backend/protocols/ping/index.html"><font face="arial, helvetica, sans-serif">-ping</font></a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="20"><a href="../../1-backend/cgi_perl/search_engine/index.html">-创建搜索引擎</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="16">-<a href="../../1adobe/GoLive/index.html">Adobe GoLive</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2"><a href="../../1-backend/cgi_perl/templates/index.html">-模板</a></td>
</tr>
<tr bgcolor="#666699">
<td colspan="2" align="center"><font color="#FFFFFF">合作伙伴</font></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" align="left">-<a href="http://www.5dmedia.com/" target="_blank">5D精英网</a></td>
</tr>
<tr align="center" bgcolor="#FFFFFF">
<td colspan="2"> <img src="../../Library/front_monkey.gif" width="59" height="68"></td>
</tr>
</tbody>
</table>
<!-- #EndLibraryItem --> </td>
<!-- End of headlines (column 1: left column) --> <!-- Gutter between columns 1 and 2 -->
<td width="10" height="794"><img src="http://www.sohu.com/images/pixel.gif" width=10></td>
<!-- Search box and directories (columns 2 and 3: middle columns, combined into one) -->
<td align=center valign=top width="558">
<div align="left"><!-- #BeginEditable "1" -->
<title>JavaScrip高级教程</title>
<strong>第四页:<font face="宋体" size="3">什么是变量</font></strong>
<p>这里是一个应用变量的例子: <br>
var happiness = "a banana split";<br>
<br>
alert("The monkeys think happiness is " + happiness);<br>
这行代码声明一个叫做happiness的变量,然后在一个alert对<br>
话框中调用这个变量。如果你看过别人的JavaScript,你可能<br>
会注意到他们在声明变量时使用了var,这种用法可能会造成<br>
问题。首先,可能会造成有些版本的 MSIE瘫痪,或运行不正<br>
常。这种情况在Mac机上的MSIE最有可能发生。其次,如果你编<br>
写很复杂的JavaScripts,你必须编写你自己的函数,所以你必<br>
须了解变量的含义。</p>
<p>正如上次的JavaScript 教程: 第四天中所述,函数是被调用后<br>
执行某特定功能的JavaScript 程序代码。最好的函数具有模块<br>
化的特性,你可以控制输入的变量以及输出的结果。而且一旦<br>
编好之后你就无需担心它会出问题,而且不会和别的函数发生<br>
冲突。要使编写地函数具备这些稳定的特性,你必须确保不要<br>
轻易改变那些作为参数传递给其他函数的变量. 下面对例子显<br>
示了如果你不注意这些细节会造成什么后果。假设我们编写一<br>
个程序将华氏温度转换为摄氏温度。点击华氏/摄氏来观察一下<br>
我所指的意思。如果你转换转换华氏50度,则会出现一条信息:<br>
“华氏50度相当于摄氏10度。”以下是代码:</p>
<p>function fahrenToCelsius(fare)<br>
{<br>
temp = (faren - 32) * 5/9;<br>
return temp;<br>
}<br>
function convertTemp()<br>
{<br>
temp = prompt("what temperature fahrenheit? ","50");<br>
celsius = fahrenToCelsius(temp);<br>
alert(temp + " degrees Fahrenheit is " +
<br>
celsius + " degrees Celsius.");<br>
} </p>
<p>这个程序很简单。一个叫做convertTemp()得函数调用另外一个<br>
叫做fahrenToCelsius()并返回结果。如果你对本程序不是很懂,<br>
你需要重新学习以下上次的javasript教程第4日。</p>
<p>关于这个例子让人困惑之处在两个函数中都有一个叫做temp得<br>
变量,在convertTemp()函数中它的作用 是储存华氏温度<br>
Fahrenheit值 (由用户提供)。在fahrenToCelsius()函数中,<br>
它用于计算转换后灯摄氏温度值Celsius。这个不仅让我们感到<br>
困惑而且也会让这个Javascript程序感到困惑。如果你试图用<br>
变量运行这段代码就会发生下面的结果:如果你希望转换华氏<br>
50度,则会显示下面的信息:"10 degrees Fahrenheit is 10<br>
degrees Celsius."(华氏10度相当于摄氏10度)。为什么你输入<br>
的是华氏50度,而程序却将其理解为你输入了10度呢?我们来<br>
研究一下这个程序的执行过程。当我们调用函数convertTemp()<br>
并在提示栏中输入了"50"时,我们得到temp = 50;然后"temp"<br>
被传递给函数farenToCelsius()。在farenToCelsius()中,参<br>
数faren被设定为50,而"temp" 被设定为(50 - 32) x 5/9, 其<br>
结果是10。在返回结果之前,各项参数值是:</p>
<p>faren = 50<br>
temp = 10</p>
<p>现在farenToCelsius()将10返回给变量celsius: </p>
<p>temp = 10<br>
celsius = 10</p>
<p>现在我们得到一个错误的语句: "10 degrees Fahrenheit is 10<br>
degrees Celsius"。如果你仔细一些,不要将两个函数中等变<br>
量起同样的名称就可以避免这些问题。但这也不是最佳解决<br>
方案。当你不断加入更多的函数时,很难确保你不会将函数中<br>
的变量重名。而且如果你重复使用许多变量名例如loop,index,<br>
count, 和the_name时,使用不同的名称实在是一个很令人头疼<br>
的事。</p>
<p>最好的解决办法是让JavaScript明白在fahrenToCelsius()函数<br>
中用到达变量temp和在convertTemp() 函数中用到的变量temp<br>
是两码事。如果每个函数都有只应用于本函数内部的变量temp<br>
你就无需担心不同函数中的同名变量会搅成一团。而var就是用<br>
于这个目的。<a href="day1_5.html">>></a></p>
<p><font face="宋体" size="3" color="#000000"><strong>JavaScript高级教程</strong></font><font color="#FF0000" face="宋体" size="3"><br>
</font><font face="宋体" size="3"><font color="#FF0000">第一页</font> <a href="day1_4.html">Javascript高级教程</a><br>
<font color="#FF0000">第二页</font> <a href="day1_2.html">本教程的课程计划</a><br>
<font color="#FF0000">第三页</font> <a href="day1_3.html">一个if-then-else的快捷方式</a><br>
<font color="#FF0000">第四页</font> 什么是变量<br>
<font color="#FF0000">第五页</font> <a href="day1_5.html">变量的使用方法</a></font></p>
<p><font size="3">[第1课][<a href="day2_1.html">第2课</a>][<a href="day3_1.html">第3课</a>][<a href="day4_1.html">第4课</a>][<a href="day5_1.html">第5课</a>]</font></p>
<!-- #EndEditable --></div>
</td>
<!-- End of search box and directories (columns 2 and 3: middle columns, combined into one) -->
<!-- Gutter between columns 3 and 4 --> <!-- Other stuff (column 4: right column) -->
<!-- End of other stuff (column 4: right column) --> </tr>
</table>
<!-- End of table surrounding page contents -->
<hr noshade size=1 width=700>
<span class=eng><br>
Copyright (C) 1998-2000 Internet Technologies China. All rights reserved.
</span>
</center>
</body>
<!-- #EndTemplate --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -