![]() |
![]() |
JavaScript高级教程 - 第五课 | |
Thau |
|
第五页:好的编程实践 编好程序的关键是程序是写给人的,不是写给计算机的。如 另一个有帮助的技术是在写代码之前作注释。这迫使你在动 第一步:写注释 //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 // } 第二步:填充代码 //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); }
最后... 总以分号结束你的每一条语句。
把变量初始化为“var”,除非你有更好的理由不这样做。
好了,既然你已经知道了如何编码,下面就让我们学习怎样使 JavaScript高级教程 Copyright © 1994-99 Wired Digital Inc. All rights reserved. |
|||||||||||||||||