Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Javascript for dummies 4th edition - Veer E.V

Veer E.V Javascript for dummies 4th edition - Wiley publishing , 2004. - 387 p.
ISBN: 0-7645-7659-3
Download (direct link): javascriptfordummies2005.pdf
Previous << 1 .. 14 15 16 17 18 19 < 20 > 21 22 23 24 25 26 .. 118 >> Next

Declaring a function
Here’s the syntax for a function declaration:
function name([parameter] [, parameter) ] [..., parameter]) {
statements
return value }
And here’s an example:
function calculateTotal(numberOrdered, itemPrice) {
var totalPrice = (numberOrdered * і temPrice) + salesTax
return totalPrice
}
This code snippet declares a calculateTotal function that accepts two arguments: numberOrdered and itemPrice. The function uses these two arguments (plus an additional variable called salesTax) to calculate the totalPrice variable, which it then returns to the JavaScript code that originally called it.
Your function can take as many arguments as you want it to (including none at all), separated by commas. You generally refer to these argument values in the body of the function (otherwise, why bother to use them at all?), so be sure to name them something meaningful. In other words, I could have substituted
x and y for numberOrdered and itemPrice, and the code would work just as well. It just wouldn’t be very easy to read or maintain!
Because the optional return statement is so important, I devote a whole section to its use. (See the section “Returning a value from a function.”)
Chapter 3: JavaScript Language Basics
Catting a function
After you declare a function, which I describe in the preceding section, you can call that function. You call a function by specifying the name of the function, followed by an open parenthesis, comma-delimited parameters, and a closing parenthesis. For example:
alert(”Total purchases come to ” + c:alculateTotal(10, 19.95))
Notice that you can embed a function call within another expression. In this example, calculateTotal(10, 19.95) is actually part of the expression being sent to the alert() method. (You find out all about methods in Chapter 4, but for now, you can think of them as special kinds of functions.)
Returning a Value from a function
You use the return statement to return a value from a function. To understand why you might want to return a value from a function, imagine yourself asking a friend to look up some information for you. If your friend went ahead and did the research but neglected to pass it along to you, you’d be pretty disappointed. Well, in this case, you’re just like a bit of JavaScript code calling a function, and your friend is the function that you’re calling. Basically, no matter how many useful things a function does, if it doesn’t return some sort of result to the piece of code that needs it, it hasn’t finished its job.
The syntax for the return keyword is simple:
return expression Here’s how it looks in action:
function calculateTotal(numberOrdered, itemPrice) {
var totalPrice = (numberOrdered * itemPrice) + salesTax return totalPrice } // Now the function is defined, so it can be called
document.write(”The total amount to remit to us is ” + calculateTotal(3, 4.99))
In this example code, the document.write() method calls the calculate Total() function. The calculateTotal() function returns the value of the totalPrice variable, which the document.write() method then displays on the Web page.
Part I: Building Killer Web Pages for Fun and Profit
Loop-the-toop
Loops are powerful constructs that you can use to reiterate a series of JavaScript statements over and over again. JavaScript supports a number of loops you can choose from, including the for loop and for-in loops, the while loop, and the do-while loop. As you see in the following section, each loop is tailored for specific kinds of situations.
The for loop
The for loop lets you step through, or traverse, a number of items quickly and easily. As an example, suppose that you want to find out whether users have a particular plug-in installed in their Web browsers. You can use the for loop to step through each of the plug-ins one by one.
First, take a peek at the generic form of the for loop.
for ([initial expression]; [condition]; [update expression]) { statements }
The for loop introduces three terms that might be new to you: the initial expression, the condition, and the update expression. Here’s how it all works:
1. The JavaScript interpreter looks at the initial expression.
The initial expression is almost always a number (usually 0 because that’s the number JavaScript arrays begin with) assigned to a variable, such as var i=0.
2. The JavaScript interpreter looks at the condition to see whether it’s true.
The condition compares the variable in Step 1 to some programmer-defined constant; for example, i <10. If the value of i is indeed less than 10, for instance, the i <10 statement is true.
3. If the value of the condition is true, the JavaScript interpreter performs all the statements in the body of the for loop, and then it evaluates the update expression.
The update expression typically increments the initial expression by 1; for example, i++ or eachOne++. (Although ++ looks kind of funny, it’s not a typo. It’s an operator that adds 1 to the variable that it’s next to. Think
Previous << 1 .. 14 15 16 17 18 19 < 20 > 21 22 23 24 25 26 .. 118 >> Next