|
Last week's handout contained more errors than I'd like to admit. I apologize. This week I tested all code examples on a demo page, so there should be fewer problems. But why waste a good oppotunity for teaching? Screwups are always instructive. Here's what you might learn from mine: 1. Confusing variable names In the original handout, I used the variable checkThis in several places where I should have used checkValue. Obviously it isn't a good idea to name variables so similarly. Try to make variable names distinct enough that you won't get them mixed up. Most important, try to make variable names meaningful. Remember that code needs to be read, usually by other people, as well as executed by the computer. 2. Failure to remember object hierarchy At several points in last week's handout I referred to document.plugins -- a quaint usage that means exactly nothing in JavaScript. The plugins array belongs not to the document object but rather to the navigator object. This mistake might remind you that the various bits and pieces of JavaScript fit together into a tree structure or hierarchy. It's important that you learn at least the major branches of that hierarchy. Most of the things you'll do in JavaScript (replacing image source, for instance) go through the document object; but things involving browser function and behavior, rather than aspects of the page, generally go through the navigator object. D'oh! 3. Treating string literal as variable name (missing quote marks) My third goof of the week involved the statement document.location=cutout.html, which should have been written document.location="cutout.html". The difference may seem subtle but it isn't. Quotation marks (single or double) around a string (in this case the URL) convert that string into a literal: a sequence of characters that mean exactly what they say and nothing more. If you don't put a literal inside quote marks, the string is interpreted as a variable -- or in this case, since it includes a period, as an expression in dot notation. This mistake teaches that little details count. It also reminds me that I often make the same mistake (like forgetting quotation marks) over and over. You should keep track of your own repeated mistakes; they're a good place to start when you're trying to debug a script. |