A Safer Version of rotateImage()

If you're not interested in perfecting your coding style, you can skip this section; it's a bit advanced. However, if you want to be a supersafe coder, read on.

At a couple of points in this book, I've mentioned that it's best to declare variables inside the functions that use them. As we saw earlier in the chapter, this won't work for variables that hold time-outs, as in ❹ of Figure 9-11. But in ❺ of Figure 9-11, the variable index, declared outside the function, does not hold a time-out. The only part of the script that uses the index variable is the rotateImage() function—so I really should be declaring index inside the rotateImage() function. Unfortunately, as you'll see in Figure 9-12, I can't.

Why Declaring ...

Get The Book of JavaScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.