27.10.2014 Views

Google Maps API 3

Create successful ePaper yourself

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

CHAPTER 3 ■ CREATING YOUR FIRST MAP<br />

Figure 3-6. Your first map<br />

Encapsulating the Code<br />

Although you now have a functioning map, you’re not quite done. You are currently cluttering the global<br />

namespace with all your variables. This might not be a big deal in a simple web page like the one you’re<br />

currently working on. But if you’re constructing a map that will be part of a bigger project, using global<br />

variables can turn out to be a big problem. The problem that can arise is that naming collisions between<br />

conflicting code occur. It’s therefore important to contain your code so that you isolate it from the<br />

outside world.<br />

To make sure that your code doesn’t clutter the global namespace, you will encapsulate it inside a<br />

self-executing anonymous function. The pattern looks like this:<br />

(function() {<br />

// The code<br />

})();<br />

41

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

Saved successfully!

Ooh no, something went wrong!