Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.5 MB, 505 trang )
50
Part I JavaWhat? The Where, Why, and How of JavaScript
Chapter 4 provides much more information about variables and their naming conventions.
For now, remember that you must pay attention to the case when you write a variable name
in JavaScript.
White Space
For the most part, JavaScript ignores white space, which is the space between statements
in JavaScript. You can use spaces, indenting, or whatever coding standards you prefer to
make the JavaScript more readable. There are some exceptions to this rule, however. Some
keywords, such as return, can be misinterpreted by the JavaScript interpreter when they’re
included on a line by themselves. You’ll see an example of this problem a little later in this
chapter.
Making programs more readable is a good enough reason to include white space. Consider
the following code sample. It includes minimal white space and indenting.
function cubeme(incomingNum) {
if (incomingNum == 1) {
return "What are you doing?";
} else {
return Math.pow(incomingNum,3);
}
}
var theNum = 2;
var finalNum = cubeme(theNum);
if (isNaN(finalNum)) {
alert("You should know that 1 to any power is 1.");
} else {
alert("When cubed, " + theNum + " is " + finalNum);
}
Now consider the same code with indenting. (You can find this code, named example1.txt, in
the Chapter 3 sample code in the companion content.)
function cubeme(incomingNum) {
if (incomingNum == 1) {
return "What are you doing?";
} else {
return Math.pow(incomingNum,3);
}
}
var theNum = 2;
var finalNum = cubeme(theNum);
if (isNaN(finalNum)) {
alert("You should know that 1 to any power is 1.");
} else {
alert("When cubed, " + theNum + " is " + finalNum);
}
Chapter 3 JavaScript Syntax and Statements
51
The second code sample performs just like the first, but it’s easier to read and follow—at
least it appears so to me! I find that it takes a short amount of time to actually write code,
but several years to work with it. When I visit the code a year later, I’m much happier when
I’ve made the code more readable and easier to follow.
Comments
Speaking of creating more readable code and maintaining that code over the long term,
comments are your friends. Code that seems blatantly obvious now won’t be nearly so obvious the next time you look at it, especially if a lot of time has passed since you wrote it.
Comments can be placed into JavaScript code in two ways: multiline and single-line.
A multiline comment in JavaScript will look familiar to you if you’ve coded in the C programming language. A multiline comment begins and ends with /* and */ respectively, as this code
example shows:
/* This is a multiline comment in JavaScript
It is just like a C-style comment insofar as it can
span multiple lines before being closed. */
A single-line comment begins with two front slashes (//) and has no end requirement,
because it spans only a single line. An example is shown here:
// Here is a single line comment.
Using multiple single-line comments is perfectly valid, and I use them for short comment
blocks rather than use the multiline comment style previously shown. For example, look at
this block of code:
// Here is another comment block.
// This one uses multiple lines.
// Each line must be preceded with two slashes.
Tip You may find it quicker to use the two-slash method for small comments that span one
line or a few lines. For larger comments, such as those at the beginning of a program or script,
the multiline comment style is a better choice because it makes adding or deleting information
easier.
Semicolons
Semicolons are used to delineate expressions in JavaScript. Technically, semicolons are not
required for most statements and expressions. However, the subtle problems that you can
encounter when you don’t use semicolons add unnecessary errors and hence unnecessary
52
Part I JavaWhat? The Where, Why, and How of JavaScript
debugging time. In some instances, the JavaScript interpreter inserts a semicolon when you
may not have wanted one at all. For example, consider this statement:
return
(varName);
In all likelihood, you wanted to write:
return(varName);
But JavaScript, acting on its own, inserts a semicolon after the return statement, making the
code appear like this to the JavaScript interpreter:
return;
(varName);
This code won’t work; the interpreter will misunderstand your intentions. If you used this
code in a function, it would return undefined to the caller, which is unlikely to be what you
want. This is an example where free use of white space is not allowed—you can’t successfully
use line breaks (explained in the next section) to separate the return keyword from the value
that it’s supposed to return.
You’ll find programming in JavaScript much easier if you use semicolons as a rule rather than
try to remember where you might not have to use them.
But you definitely shouldn’t use semicolons in one instance: when using loops and conditionals.
Consider this bit of code:
if (a == 4)
{
// code goes here
}
In this case, you wouldn’t use a semicolon at the end of the if statement. The reason is that
the statement or block of statements in opening and closing braces that follows a conditional
is part of the conditional statement, in this case, the if statement. A semicolon marks the
end of the if statement, and if improperly placed, dissociates the first part of the if statement
from the rest of it. For example, the following code is wrong (the code within the braces will
execute regardless of whether a equals 4):
if (a == 4);
{
// code goes here
}
Tip When opening a loop or function, skip the semicolons.
Chapter 3 JavaScript Syntax and Statements
53
Line Breaks
Related closely to white space and even to semicolons in JavaScript are line breaks, sometimes called carriage returns. Known in the official ECMA-262 standard as “Line Terminators,”
these characters separate one line of code from the next. Like semicolons, the placement
of line breaks matters. As you saw from the example in the previous section, placing a line
break in the wrong position can result in unforeseen behavior or errors.
Not surprisingly, the most common use of line breaks is to separate individual lines of code
for readability. You can also improve readability of particularly long lines of code by separating them with line breaks. However, when doing so, be aware of issues like the one illustrated
by the return statement cited earlier, in which an extra line break can have unwanted effects
on the meaning of the code.
Placing JavaScript Correctly
JavaScript can be placed in a couple of locations within a Hypertext Markup Language
(HTML) page: in the