O'Reilly logo

The Book of JavaScript, 2nd Edition by thau!

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required