|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
Avoiding BugsA bug is an error in a program that prevents it from doing what it should do. If you've tried writing any scripts on your own, you've probably run into one or more bugs. If not, you willno matter how careful you are. Although you'll undoubtedly run into a few bugs if you write a complex script, you can avoid many others by carefully writing and double-checking your script. Using Good Programming PracticesThere's not a single programmer out there whose programs always work the first time, without any bugs. However, good programmers share a few habits that help them avoid some of the more common bugs. Here are a few good habits you can develop to improve your scripts:
Avoiding Common MistakesAlong with following good scripting practices, you should also watch for common mistakes in your scripts. Different people make different mistakes in JavaScript programming, but the following sections explore some of the most common ones. Syntax ErrorsA syntax error is an incorrect keyword, operator, punctuation mark, or other item in a script. Most often, it's caused by a typing error. Typical syntax errors include mistyped commands, missing parentheses, and functions with the wrong number of arguments. Syntax errors are usually obviousboth to you when you look at the script and to the browser's JavaScript interpreter when you load the script. These errors usually result in an error message and can easily be corrected. Assignment and EqualityOne of the most common syntax errors made by beginning JavaScript programmers is confusing the assignment operator (=) with the equality operator (==). This can be a hard error to spot because it might not result in an error message. If you're confused about which operator to use, follow this simple rule: Use = to change the value of a variable, and use == to compare two values. Here's an example of a statement that confuses the two: If (a = 5) alert("found a five.");The statement looks logical enough, but a = 5 will actually assign the value 5 to the a variable rather than compare the two. The browser usually detects this type of error and displays an error message in the JavaScript console, but the opposite type of error (using == when you mean =) may not be detected. Local and Global VariablesAnother common mistake is confusing local and global variables, such as trying to use the value of a variable that was declared in a function outside the function. If you actually need to do this, you should either use a global variable or return a value from the function. By the Way Hour 5, "Using Variables, Strings, and Arrays," describes the differences between local and global variables in detail. Using Objects CorrectlyAnother common error is to refer to JavaScript objects incorrectly. It's important to use the correct object names and to remember when to explicitly name the parent of an object. For example, you can usually refer to the window.alert() method as simply alert(). However, there are some cases when you must use window.alert(), such as in some event handlers. If you find that alert() or another method or property is not recognized by the browser, try specifying the window object. Another common mistake is to assume that you can omit the document object's name, such as using write() instead of document.write(). This won't work because most scripts have a window object as their scope. HTML ErrorsLast but not least, don't forget that JavaScript isn't the only language that can have errors. It's easy to accidentally create an error in an HTML documentfor example, forgetting to include a closing </table> tag, or even a closing </script> tag. Although writing proper HTML is beyond the scope of this book, you should be aware that sometimes improper HTML can cause errors in your JavaScript. When you experience bugs, be sure to double-check the HTML, especially the objects (such as forms or images) that your script manipulates. Watch Out Your script can also introduce HTML errors if it modifies the DOM, particularly if it uses the innerHTML property. Double-check HTML produced by a script to avoid these problems. |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |