⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 day5_2.html

📁 JAVASCRIPT 高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  </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 size="3">打印变量</font></strong> 
      <p>一旦你发现一个错误,就可以清除它。不幸的是,发现它们的<br>
        出处并不总是很容易 - 你的大部分调试时间只是花在指出错<br>
        误的位置。</p>
      <p>最可靠的方法之一是在你的代码中加入一些简单的语句打印出<br>
        正在发生什么。假如你在下面的两段程序中发现一个问题:</p>
      <pre>function getName()
{
    var first_name = prompt(&quot;what's your first name?&quot;,&quot;&quot;);
    var last_name = prompt(&quot;what's your last name?&quot;,&quot;&quot;);
    var the_name = first_name + &quot; &quot; + last_name;
	
}

function theGreeting()
{
    var the_name = &quot;&quot;;
    the_name = getName();

    if (the_name == &quot;Dave Thau&quot;)
    {
	alert(&quot;Hello, oh greatest one!&quot;);
    } else {
	alert(&quot;Ahoy palloi!&quot;);
    }
}</pre>
      <p>运行这段程序,看看你是否能发现出了什么问题(Netscape 3.x<br>
        的用户可能会遇到一些错误检查的问题,这是由于Netscape 3.x<br>
        本身的原因,以下的Javascript例子与此相同)。如果你在警告<br>
        对话框中随意输入一些名字,你会得到问候:“Ahoy palloi!”。<br>
        但是,如果你在第一个提示对话框中输入“Dave”,在第二个中<br>
        输入“Thau”,你应该得到“Hello,oh greatest one!”这条<br>
        信息。然而,你还是只得到“Ahoy palloi!”这条信息。很明显,<br>
        函数中出了错误。在这个简单的例子程序中,你或许只是查看<br>
        JavaScript代码就能发现错误。然而,当你的代码变得越来越复<br>
        杂时,只靠目测来发现错误会变得愈加困难。</p>
      <p>如果JavaScript没能捕获你的错误,你也没有通过查看代码发现<br>
        错误,有时打印出变量会对你有所帮助。最简单的方法是象下面<br>
        这样使用一个alert():</p>
      <p>// theGreeting gets a name using getName, then presents <br>
        // one or two alert boxes depending on what the name is<br>
        //function getName()<br>
        {<br>
        &nbsp;&nbsp;&nbsp; var first_name = prompt(&quot;what's your first name?&quot;,&quot;&quot;);<br>
        &nbsp;&nbsp;&nbsp; var last_name = prompt(&quot;what's your last name?&quot;,&quot;&quot;);<br>
        &nbsp;&nbsp;&nbsp; var the_name = first_name + &quot; &quot; + last_name; 
        <br>
        &nbsp;&nbsp;&nbsp; alert(&quot;in getName, the_name is: &quot; + the_name);</p>
      <p>}<br>
        <br>
        // theGreeting gets a name using getName, then presents <br>
        // one of two alert boxes depending on what the name is<br>
        // function theGreeting()<br>
        {<br>
        &nbsp;&nbsp; var the_name = &quot;&quot;;<br>
        &nbsp;&nbsp; the_name = getName();<br>
        &nbsp;&nbsp; alert(&quot;after getName, the_name = &quot; + the_name);<br>
        &nbsp;&nbsp; if (the_name == &quot;Dave Thau&quot;)<br>
        &nbsp;&nbsp; {<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(&quot;hello, oh greatest one!&quot;);<br>
        &nbsp;&nbsp; }else{ <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(&quot;ahoy palloi!&quot;); 
        <br>
        &nbsp;&nbsp; }<br>
        }</p>
      <p>请注意我们已经在所有重要的地方加入警告语句。现在试着运行<br>
        这段程序。如果你输入名称“Dave”和“Thau”,你会注意到第<br>
        一个警告显示“in getName, the_name is: Dave Thau,”,但<br>
        是第二个警告显示“after getName, the_name = undefined,”,<br>
        这就告诉你在getName()的最后一行事情变得糟糕起来。不知何<br>
        故,the_name只在函数存在前正确,但是theGreeting没有给变<br>
        量the_name正确赋值。当你写的函数能正确运行,但返回值出现<br>
        问题时,你最先要做的就是检查你是否的确让其返回了一个值。<br>
        很明显,问题就出在这儿。getName()函数指出了名称,但没有<br>
        返回它。所以我们应把语句“return the_name;”加到函数末尾。</p>
      <p>把一些警告对话框加入你的代码中是很有帮助的。不幸的是,每<br>
        隔一行就按一次“OK”也是一种痛苦。</p>
      <p>不用警告对话框也能调试代码。一种选择是把调试信息写到窗体<br>
        的一个文本区内。另一种可能是把调试信息写在另一个窗口上。<br>
        这儿有一个把调试信息写在下面文本区的调试代码的例子。</p>
      <p>使你的调试经历更舒适的第三个诀窍是这样的:创建不同的调试<br>
        等级,然后设置“调试”变量。下面就是在此页上运行的<br>
        JavaScript代码:</p>
      <p>// debug can be either none, alert, or textarea <br>
        //&nbsp; depending on the kind of debugging I want to do<br>
        // var debug = &quot;none&quot;;<br>
        // function getName gets a first and last name, <br>
        // concatenates them with a space in between, <br>
        // and returns the name function getName()<br>
        { <br>
        &nbsp;&nbsp;&nbsp; var first_name = prompt(&quot;what's your first name?&quot;,&quot;&quot;);<br>
        &nbsp;&nbsp;&nbsp; var last_name = prompt(&quot;what's your last name?&quot;,&quot;&quot;);<br>
        &nbsp;&nbsp;&nbsp; var the_name = first_name + &quot; &quot; + last_name; 
        <br>
        &nbsp;&nbsp;&nbsp; var error_message = &quot;in getName, the_name is: 
        &quot; + the_name; <br>
        &nbsp;&nbsp;&nbsp; doError(&quot;in getName, the_name is: &quot; + the_name);<br>
        }<br>
        <br>
        // theGreeting gets a name using getName, then presents <br>
        // one of two alert boxes depending on what the name is<br>
        // function theGreeting()<br>
        {<br>
        &nbsp;&nbsp; var the_name = &quot;&quot;;<br>
        &nbsp;&nbsp; the_name = getName(); <br>
        &nbsp;&nbsp; doError(&quot;after getName, the_name = &quot; + the_name);<br>
        &nbsp;&nbsp; if (the_name == &quot;Dave Thau&quot;)<br>
        &nbsp;&nbsp; {<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(&quot;hello, oh greatest 
        one!&quot;);<br>
        &nbsp;&nbsp; } else { <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(&quot;ahoy palloi!&quot;);<br>
        &nbsp;&nbsp; }<br>
        }<br>
        <br>
        // doError is the error handling routine<br>
        // depending on the type of debug message<br>
        // it presents either no debug message, an alert<br>
        // or puts the message in a textarea<br>
        //<br>
        function doError(the_message)<br>
        {<br>
        &nbsp;&nbsp;&nbsp; if (debug == &quot;alert&quot;) <br>
        &nbsp;&nbsp;&nbsp; {<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(the_message);<br>
        &nbsp;&nbsp;&nbsp; } else if (debug == &quot;textarea&quot;)<br>
        &nbsp;&nbsp;&nbsp; { <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; window.document.the_form.the_text.value 
        += &nbsp;&nbsp; the_message + &quot;&lt;br&gt;\n&quot;; <br>
        &nbsp;&nbsp;&nbsp; }<br>
        }<br>
        <br>
        &nbsp;&nbsp;&nbsp; 请注意我已经定义了一个叫“debug”的变量,它可以是<br>
        “none”,“alert”或“textarea”。于是当我想产生一个错<br>
        误信息时,我把它送给函数doError(),此函数可能什么也不做,<br>
        或者显示一个消息对话框,或者把消息粘贴到一个文本区中,这<br>
        取决于我怎样设置调试变量。当你想同时看到多条错误信息时,<br>
        你可以设置调试变量为“textarea”。当你准备把你的代码显示<br>
        给全世界时,你只需要把调试变量设为“none”,于是错误信息<br>
        将不再出现,这样可以省去发现和清除所有调试语句的麻烦。</p>
      <p>通常,程序员可以创建不同的调试等级,如“none”,“brief”<br>
        和“extreme”。“brief”将打印一些调试信息,“extreme”<br>
        将打印大量调试信息,“none”当然不会打印任何信息。</p>
      <p>如果你按此方法建立你的调试系统,你可以在编码时把调试等级<br>
        设为“brief”,在准备公布你的JavaScript时把调试变量设为<br>
        “none”。如果有不可思议的事情发生,并且你不知道去哪儿发<br>
        现问题,你可以把调试等级设为“extreme”,然后流览所有的<br>
        调试信息直到发现可疑的地方。</p>
      <p>好了,调试系统就讨论到这儿。现在让我们看看JavaScript编码<br>
        器产生的一般性错误。<a href="day5_3.html">&gt;&gt;</a></p>
      <p align="left"><font face="宋体" size="3" color="#000000"><strong>JavaScript高级教程</strong></font><font color="#FF0000" face="宋体" size="3"><br>
        </font><font color="#FF3300" size="3">第一页</font><font size="3"> </font><a href="day5_1.html"><font size="3" face="verdana, arial, geneva, sans-serif">JavaScript</font><font size="3">高级教程</font><font size="3" face="verdana, arial, geneva, sans-serif">- 
        </font><font size="3">第</font><font size="3" face="verdana, arial, geneva, sans-serif">5</font><font size="3">天</font></a><font size="3"> 
        <br>
        </font><font color="#FF3300" size="3">第二页 </font><font size="3">打印变量<br>
        </font><font color="#FF3300" size="3">第三页 </font><font size="3"><a href="day5_3.html">一般性程序错误</a><br>
        </font><font color="#FF3300" size="3">第四页 </font><font size="3"><a href="day5_4.html">修正错误</a><br>
        </font><font color="#FF3300" size="3">第五页 </font><font size="3"><a href="day5_5.html">好的编程实践</a><br>
        </font><font color="#FF3300" size="3">第六页 </font><font size="3"><a href="day5_6.html">按速度优化JavaScript代码</a><br>
        </font><font color="#FF3300" size="3">第七页 </font><a href="day5_7.html"><font size="3">下面讲什么?</font></a></p>
      <p align="left">[<a href="day1_1.html">第1课</a>][<a href="day2_1.html">第2课</a>][<a href="day3_1.html">第3课</a>][<a href="day4_1.html">第4课</a>][第5课]</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.&nbsp; All rights reserved. 
 </span> 
</center>
</body>
<!-- #EndTemplate --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -