📄 day5_5.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" rowspan="2"><small><small><br> </small></small><strong>第五页:<font size="3">好的编程实践</font></strong> <p>编好程序的关键是程序是写给人的,不是写给计算机的。如<br> 果你能明白其他人或许会阅读你的JavaScript,你就会写更<br> 清晰的代码。代码越清晰,你就越不容易犯错误。机灵的代<br> 码是可爱的,但就是这种机灵的代码会产生错误。最好的经<br> 验法则是KISS,即Keep It Simple,Sweetie(保持简单,可爱)。</p> <p>另一个有帮助的技术是在写代码之前作注释。这迫使你在动<br> 手之前先想好。一旦写好了注释,你就可以在其下面写代码。<br> 下面是一个用这种方法写函数的例子:</p> <p><strong>第一步:写注释</strong></p> <blockquote> <pre>//function beSassy()// beSassy asks for a user's name, chooses a random // insult and returns an alert box with the user's name and the// insult.function beSassy(){ // first write a list of insults // // next get the user's name // // then choose a random insult // // finally, return the personalized sass //}</pre> </blockquote> <p><strong>第二步:填充代码</strong></p> <blockquote> <pre>//function beSassy()// beSassy asks for a user's name, chooses a random // insult and returns an alert box with the user's name and the// insult.function beSassy(){ // first write a list of insults // var the_insult_list = new Array; the_insult_list[0] = "your shoe lace is untied"; the_insult_list[1] = "your mama!"; the_insult_list[2] = "it's hard to be insulting"; // next get the user's name // var the_name = prompt("What's your name?", ""); // then choose a random insult // var the_number = Math.random() * 5; var insult_number = parseInt(the_number); var the_insult = the_insult_list[insult_number]; // finally, return the personalized sass // alert("Hey " + the_name + " " + the_insult);}</pre> </blockquote> <blockquote> <p>这种先写注释的策略不仅迫使你在写代码前思考,而且<br> 使编码的过程看起来容易些 - 通过把任务分成小的,<br> 易于编码的各个部分,你的问题看起来就不太象珠穆朗<br> 玛峰,而象一群令人愉悦的起伏的小山。</p> </blockquote> <p><strong>最后...</strong></p> <p>总以分号结束你的每一条语句。</p> <blockquote> <p>虽然并不是严格必需,你应该养成以分号结束每一条语<br> 句的习惯,这样可以避免这行后面再有代码。忘了加<br> 分号,下一行好的代码会突然产生错误。</p> </blockquote> <p>把变量初始化为“var”,除非你有更好的理由不这样做。</p> <blockquote> <p>用“var”把变量局域化可以减少一个函数与另一个不相<br> 关函数相混淆的机会。</p> </blockquote> <p>好了,既然你已经知道了如何编码,下面就让我们学习怎样使<br> 你的JavaScript快速运行。<a href="day5_6.html">>></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"><a href="day5_2.html">打印变量</a><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">好的编程实践<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> <hr align="left"> <!--webbot bot="Include" U-Include="../../copyright.html" TAG="BODY" startspan --> <p><font face="verdana, arial, geneva, sans-serif" size="2"><a href="http://phtshop.yeah.net" target="_top">本文根据 网猴 相关文章改编,版权归原作者所有。</a> </font></p> <!--webbot bot="Include" endspan i-checksum="63119" --> </td> </tr> <tr> </tr> </table></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -