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

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

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

alter program flow. A variety of loops can be formed using while, for, or do-while in order to<br />

iterate a particular piece of code. Further program flow control can be achieved with break and<br />

continue. As larger scripts are built using the constructs presented in this chapter, repetitive<br />

code is often introduced. To eliminate redundancy and create more modular programs,<br />

functions—the topic of the next chapter—should be employed.<br />

Chapter 5: Functions<br />

<strong>JavaScript</strong> functions can be used to create script fragments that can be used over and over<br />

again. When written properly, functions are abstract—they can be used in many situations and<br />

are ideally completely self-contained, with data passing in and out through well-defined<br />

interfaces. <strong>JavaScript</strong> allows for the creation of such functions, but many developers avoid<br />

writing code in such a modular fashion and rely instead on global variables and side-effects to<br />

accomplish their tasks. This is a shame, because <strong>JavaScript</strong> supports all the features<br />

necessary to write modular code using functions and even supports some advanced features,<br />

such as variable parameter lists. This chapter presents the basics of functions, and the next two<br />

chapters discuss how, underneath it all, the real power of <strong>JavaScript</strong> comes from objects!<br />

Function Basics<br />

<strong>The</strong> most common way to define a function in <strong>JavaScript</strong> is by using the function keyword,<br />

followed by a unique function name, a list of parameters (that might be empty), and a statement<br />

block surrounded by curly braces. <strong>The</strong> basic syntax is shown here:<br />

function functionname(parameter-list)<br />

{<br />

}<br />

statements<br />

A simple function that takes no parameters called sayHello is defined here:<br />

function sayHello()<br />

{<br />

}<br />

alert("Hello there");<br />

To invoke the function somewhere later in the script, you would use the statement<br />

sayHello();<br />

Note Forward references to functions are generally not allowed; in other words, you should<br />

always define a function before calling it. However, in the same tag within<br />

which a function is defined you will be able to forward-reference a function. This is a very<br />

poor practice and should be avoided.<br />

Parameter-Passing Basics

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

Saved successfully!

Ooh no, something went wrong!