↑
Main Page
Prefix increment/decrement
If you place this line of code into an HTML page, and click the link, you see
“[Object]”
printed on the
screen (Figure 2-3). This occurs because
window.open()
returns a reference to the newly opened win-
dow (this and other methods of the window are discussed further in Chapter 5, “JavaScript in the
Browser”). That object is then converted to a string for display.
Figure 2-3
To avoid this, use the
window.open()
call with the void operator:
<a href=”javascript:void(window.open(‘about:blank’))”>Click Me</a>
This makes the
window.open()
call return
undefined
, which is not a valid value and is not displayed
in the browser window. Remember, functions that have no return value actually return
undefined
.
Prefix increment/decrement
Two operators taken directly from C (and Java) are prefix increment and prefix decrement. Prefix
increment, which adds one to a number value, is indicated by placing two plus signs (++) in front of a
variable:
var iNum = 10;
++iNum
The second line increments
iNum
to
11
. This is effectively equal to:
var iNum = 10;
iNum = iNum + 1;
Likewise, the prefix decrement subtracts one from a value. The prefix decrement is indicated by two
minus signs (– –) placed before the variable:
var iNum = 10;
--iNum;
In this example, the second line decreases the value of
iNum
to
9
.
When you use prefix operators, note that the increment/decrement takes place
before
the expression is
evaluated. Consider the following example:
34
Chapter 2
05_579088 ch02.qxd 3/28/05 11:35 AM Page 34
Free JavaScript Editor
Ajax Editor
©
→