27.10.2014 Views

Google Maps API 3

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 3 ■ CREATING YOUR FIRST MAP<br />

underscores instead of spaces. Another technique is to use camelCasing, which means that each new<br />

word (except the first one) in the variable name starts with an uppercase letter.<br />

var variable name;<br />

var variableName;<br />

Also note that variable names are case sensitive, so variableName and variablename will refer to two<br />

different variables.<br />

■ Warning Always use the var keyword when defining variables. It’s possible to omit it, but doing so<br />

automatically makes the variable global, which means that it will be available everywhere. This might sound<br />

convenient but is in fact the source of many errors and problems, so always try to avoid it. To read more about<br />

why this is bad, read Douglas Crockford’s excellent article “Global Domination” at www.yuiblog.com/blog/<br />

2006/06/01/global-domination/.<br />

Common Data Types<br />

Even if JavaScript is a loosely typed language, it still has different data types. I will briefly describe the<br />

three most basic ones.<br />

Strings<br />

Strings are text. When you define a string, you can use either single or double quote marks around it. I<br />

tend to use single quote marks, but that’s just a matter of personal preference.<br />

var fruit = 'apple';<br />

var fruit = "apple";<br />

But what if you need to have a single or double quote in the string? In these cases, you can use<br />

something called escaping. What this means is that a backslash (\) is inserted before the character being<br />

escaped. The JavaScript interpreter will then know that the character that follows it is part of the string<br />

and discard any other meaning it might have.<br />

var text = 'I\'m hungry';<br />

var myScreen = "I have a 22\" screen";<br />

Worth mentioning also is that to escape a backslash, you put a backslash in front of it.<br />

Numbers<br />

Numbers aren’t enclosed inside quote marks but are simply assigned. All kinds of numbers can be used.<br />

var myAge = 37;<br />

var temperature = -3;<br />

var processorSpeed = 2.66;<br />

33

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

Saved successfully!

Ooh no, something went wrong!