Web Development with jQuery®

(Elliott) #1

(^22) ❘ CHAPTER 1 INTRODUCTION TO JQUERY
event? event.preventDefault() : (window.event.returnValue = false);
};
}
};
In the One True Brace convention, the function assigned to window.onload follows the Allman Style,
while the code within it follows K&R Style.
When I write JavaScript code, I prefer a mixture of Allman Style and K&R Style. I use Allman Style
for all function and class defi nitions as well as control structures, and I use K&R Style for array and
object defi nitions (JSON), and function calls. In practice this looks something like this:
$(document).ready(
function()
{
$('a#examplePumpkin').click(
function(event)
{
event.preventDefault();
window.open(
'pumpkin.jpg',
'Pumpkin',
'scrollbars=no,width=300,height=280,resizable=yes'
);
}
);
}
);
Which programming convention you use is, of course, a matter of personal taste. Often which
convention to use can lead to endless battles among programming teams; sometimes people have
different tastes. How you indent your code can be a touchy and personal topic. You should use
whichever convention makes the most sense for you. Although the methods I’ve showcased are the
most popular, there are a multitude of variations that exist out there in the real world. You can fi nd
more information about programming indention styles on Wikipedia at http://en.wikipedia.org/
wiki/Indent_style.
Optional Curly Braces and Semicolons
In the previous conventions, you’ll note that there is always a single space between the keyword that
begins the control structure, like if and the opening parenthesis. The following is a switch control
structure using the fi rst convention:
switch (variable) {
case 1 :
condition = 'this';
break;
case 2 :
condition = 'that';
http://www.it-ebooks.info

Free download pdf