23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 4 ■ CONTROLLING FLOW<br />

98<br />

statement following its boolean expression. In the following if statement, run(miles); is the one child<br />

statement permitted by JavaScript:<br />

if (timeToRun === true) run(miles);<br />

Oftentimes this will not do, and JavaScript knows this. If you bundle several child statements in a<br />

pair of curly braces, JavaScript will look the other way and view the bundle, referred to as a block, as one<br />

child statement. So if I want JavaScript to run three child statements whenever it’s time to run, which is<br />

to say timeToRun contains true, then I can bundle those statements in a block. JavaScript will be happy as<br />

a clam, and I get to run in shoes rather than barefoot:<br />

if (timeToRun === true) {<br />

lace(shoes);<br />

run(miles);<br />

shower();<br />

}<br />

Note that the block of child statements is not followed by a semicolon. However, the child<br />

statements within the block are.<br />

Writing an if Condition<br />

Oftentimes, you will want JavaScript to run a path if circumstances permit but otherwise do nothing and<br />

move on. if conditional statements will be your bread and butter for this type of decision. To write one<br />

of these statements, simply type the keyword if, followed by an expression in parentheses, and then a<br />

path in the form of a child statement or block. In the event that the expression does not return a<br />

boolean, JavaScript will convert the value to a boolean by passing it to Boolean(), which we explored in<br />

Chapter 2. So if the expression returns any value other than undefined, null, "", 0, NaN, or false,<br />

JavaScript has a green light to run the path.<br />

Therefore, a JavaScript interpreter views an if condition like so:<br />

if (Boolean(expression)) path<br />

But you write it like this:<br />

if (expression) path<br />

Open firebug.html in Firefox and then press F12 to enable Firebug. If you’re just joining us, flip back<br />

to the Preface for details on how to do this.<br />

For any fast running I do, I tend to wear the Nike Mayfly, which weighs just four ounces. By<br />

comparison, most running shoes weigh three or four times as much. However, the downside to the<br />

Mayfly’s minimalist design is that its cushioning goes dead after just 100 kilometers.<br />

Let’s create a mayfly object containing two methods that may query a secret variable named tally,<br />

which will contain a tally of kilometers run in a pair of Mayfly shoes. mayfly.addToTally() adds its<br />

parameter (named km) to tally only if km is safe for addition—that is to say, if km is of the number<br />

datatype but not the special numbers NaN or Infinity. The other method, mayfly.kmLeftToLive(), will<br />

return a message indicating how many kilometers of cushioning the Mayfly has left only if tally is less<br />

than 100.<br />

So in Firebug, enter the following code, and click Run. Doing so creates a closure so that tally may<br />

be queried only by addToTally() and kmLeftToLive(). Closures are covered in Chapter 6, so just nod<br />

knowingly for now. Anyway, just focus on the two if conditions.<br />

var mayfly = function () {<br />

var tally = 0;<br />

return {

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!