11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

This is not a problem in itself. <strong>The</strong> problem comes when novice programmers see HTML event<br />

handlers referenced in two different ways (like ONCLICK and onClick in the previous example)<br />

and assume event handlers can be accessed similarly in <strong>JavaScript</strong>. This is not the case. <strong>The</strong><br />

corresponding event handler in <strong>JavaScript</strong> is onclick, and it must always be referred to as<br />

such. <strong>The</strong> reason that ONCLICK and onClick work in HTML is that the browser automatically<br />

binds them to the correct onclick event handler in <strong>JavaScript</strong>.<br />

Consider also the following two tags, which are not equivalent:<br />

<br />

<br />

<strong>The</strong> reason they are not equivalent is that the first modifies the variable x, while the second<br />

modifies X. Because <strong>JavaScript</strong> is case-sensitive, these are two distinct variables. This<br />

illustrates an important aspect of HTML attributes: while the attribute name is not casesensitive,<br />

its value may be. <strong>The</strong> onclick HTML attribute is not case-sensitive and so may be<br />

written onClick, ONCLICK, or even oNcLiCk. However, because the value to which it is set<br />

contains <strong>JavaScript</strong>, its value is case-sensitive.<br />

Fortunately, with the rise of XHTML, which requires that element and attribute names be written<br />

in lowercase, the case sensitivity issue at the intersection between the two technologies is less<br />

murky. Developers should always assume case sensitivity and as far as markup goes,<br />

lowercase should always be favored.<br />

Whitespace<br />

Whitespace characters are those characters that take up space on the screen without any<br />

visible representation. Examples include ordinary spaces, tabs, and linebreak characters. Any<br />

sequence of excessive whitespace characters is ignored by <strong>JavaScript</strong>. For example<br />

x = x + 1;<br />

is the same as<br />

x = x + 1;<br />

This suggests that the use of whitespace is more for the benefit of the programmer than the<br />

interpreter. Indeed, thoughtful use of whitespace to offset comments, loop contents, and<br />

declarations results in more readable and understandable code.<br />

Note Because of <strong>JavaScript</strong>’s ambivalence to whitespace and most Web users’ frustration with<br />

slow download times, some <strong>JavaScript</strong> programmers choose to ―compress‖ their scripts<br />

by removing excess whitespace characters either by hand or using a tool.<br />

<strong>The</strong> spacing between tokens can be omitted if the meaning is unambiguous. For example,<br />

x=<br />

contains no spaces, but is acceptable because its meaning is clear. However, most operations<br />

other than simple arithmetic functions will require a space to indicate the desired meaning.<br />

Consider the following:<br />

s = typeof x;<br />

s = typeofx;

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

Saved successfully!

Ooh no, something went wrong!