FREE BOOK

Chapter 2: How to code a JavaScript application

Posted by Murach Free Book | Internet & Web November 02, 2009
This chapter presents a subset of JavaScript and DOM scripting that will soon have you writing significant applications. If you don't have any programming experience, this chapter also makes a great aptitude test. If you read it and can do the exercises at the end of the chapter, you're ready for the rest of this book.

How to create identifiers

Variables, functions, objects, properties, methods, and events must all have names so you can refer to them in your JavaScript code. An identifier is the name given to one of these components.

Figure 2-6 shows the rules for creating identifiers in JavaScript. Besides the first four rules, you can't use any of the JavaScript reserved words (also known as keywords) as an identifier. These are words that are reserved for use within the JavaScript language. You should also avoid using any of the JavaScript global properties or methods as identifiers. Although you can sometimes get away with this, it will lead to problems in some applications.

Besides the rules, you should give your identifiers meaningful names. That means that it should be easy to tell what an identifier refers to and easy to remember how to spell the name. To create names like that, you should avoid abbreviations. If, for example, you abbreviate the name for monthly investment as mon_inv, it will be hard to tell what it refers to and hard to remember how you spelled it. But if you spell it out as monthly_investment, both problems are solved.

Similarly, you should avoid abbreviations that are specific to one industry or field of study unless you are sure the abbreviation will be widely understood. For example, mpg is a common abbreviation for miles per gallon, but cpm could stand for any number of things and should be spelled out.

To create an identifier that has more than one word in it, most JavaScript programmers use a convention called camel casing. With this convention, the first letter of each word is uppercase except for the first word. For example, monthlyInvestment and taxRate are identifiers that use camel casing.

The alternative is to use underscore characters to separate the words in an identifier. For example, monthly_investment and tax_rate use this convention. If the standards in your shop specifies one of these conventions, by all means use it. Otherwise, you can use either convention. In either case, though, be consistent.

Rules for creating identifiers in JavaScript

  • Identifiers can only contain letters, numbers, the underscore, and the dollar sign.
  • Identifiers can't start with a number.
  • Identifiers are case-sensitive.
  • Identifiers can be any length.
  • Identifiers can't be the same as reserved words.
  • Avoid using global properties and methods as identifiers. If you use one of them, you won't be able to use the global property or method that the identifier refers to.
  • Avoid using names that are similar to reserved words, global properties, or global methods. For example, avoid using "Switch" since it is similar to "switch".
Reserved words in JavaScript (words for future use are italicized)
abstract else instanceof switch
boolean enum int synchronized
break export interface this
byte extends long throw
case false native throws
catch final new transient
char finally null true
class float package try
const for private typeof
continue function protected var
debugger goto public void
default if return volatile
delete implements short while
do import static with
double in super  

Global properties and methods in JavaScript
alert focus prompt
blur getComputedStyle resizeBy
clearInterval Infinity resizeTo
clearTimeout isFinite scroll
close isNaN scrollBy
confirm moveBy scrollTo
decodeURI moveTo setInterval
decodeURIComponents NaN setTimeout
encodeURI open undefined
encodeURIComponents parseFloat unescape
escape parseInt  
eval print  

Valid identifiers in JavaScript
subtotal index_1 $
taxRate calculate_click $log

Description

  • Identifiers are the names given to variables, functions, objects, properties, and methods.

Figure 2-6 How to create identifiers

Total Pages : 20 56789

comments