06.05.2013 Views

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

SHOW MORE
SHOW LESS

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

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

[ 35 ]<br />

Chapter 2<br />

• Indenting should be done with two spaces (<strong>and</strong> no tabs). This keeps the code<br />

<strong>com</strong>pact, but still clear.<br />

•<br />

Comments should be used wherever necessary.<br />

° Doxygen-style documentation blocks ( /** ... */) should be<br />

used to <strong>com</strong>ment files <strong>and</strong> functions.<br />

°<br />

°<br />

Any <strong>com</strong>plex or potentially confusing code should be<br />

<strong>com</strong>mented with // or /* ... */.<br />

Comments should be written in sentences with punctuation.<br />

• Control structure keywords ( if, else, for, switch, <strong>and</strong> so on) should appear<br />

at the beginning of a line, <strong>and</strong> be followed by a single space (if (), not<br />

if()). Here's an example:<br />

if (a) {<br />

}<br />

// Put code here.<br />

else if (b) {<br />

}<br />

// Put code here.<br />

else {<br />

}<br />

// Put code here.<br />

• Operators ( +, =, *, &&, ||, <strong>and</strong> so on) should have a single space on each side,<br />

for example: 1 + 2. The exception to this rule is the member operator (.),<br />

which is used to access a property of an object. There should be no spaces<br />

surrounding these. Example: window.document (never window . document).<br />

Stylistic differences between PHP <strong>and</strong> <strong>JavaScript</strong><br />

Not all PHP coding st<strong>and</strong>ards apply to <strong>JavaScript</strong>. PHP variables <strong>and</strong> function names<br />

are declared in all lower case with underscores (_) to separate words. <strong>JavaScript</strong><br />

typically follows different conventions.<br />

<strong>JavaScript</strong> variables <strong>and</strong> functions are named using camel case (sometimes called<br />

StudlyCaps). For a variable or function, the first word is all lower case. Any<br />

subsequent words in the variable or function name are capitalized. Underscores<br />

are not used to separate words. Here are some examples:<br />

var myNewVariable = "Hello World";<br />

function helloWorld() {<br />

alert(myNewVariable);<br />

}<br />

This material is copyright <strong>and</strong> is licensed for the sole use by Richard Ostheimer on 18th June 2009<br />

2205 hilda ave., , missoula, , 59801

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

Saved successfully!

Ooh no, something went wrong!