Books
in black and white
Main menu
Home About us Share a book
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 .. 19 20 21 22 23 24 < 25 > 26 27 28 29 30 31 .. 118 >> Next

The following code first creates an instance of pet and names that instance Boots, and then it calls the talk() method associated with Boots.
Boots = new pet("Boots”, "cat”, "orange striped”);
Boots.talk;
56
Part I: Building Killer Web Pages for Fun and Profit
Here’s how the JavaScript interpreter executes these two JavaScript statements:
^tco
1. The first statement passes three variables to the pet() constructor function and assigns the resulting object to the Boots variable.
When this first statement finishes processing, the Boots variable contains an object associated with the following three properties:
• Boots.name = "Boots"
• Boots.kind = "cat"
• Boots.color = "orange striped"
2. The second statement (Boots.talk) passes the value of Boots.kind, which is "cat", to the ftalk() function.
3. The ftalk() function contains an if statement that says, “If the input variable is cat, print meow-meow-meow to the screen.”
So, because the string "cat" was passed to the ftalk() function, you
see meow-meow-meow on the screen.
If creating your own objects and methods isn’t clear to you right now, it will be after you’ve had a chance to load and play with the ch2_new_this.htm file, located on the companion CD.
Working with Variables
A variable is a named placeholder for a value. You use the var keyword to construct an expression that first declares a variable and then (optionally) initializes its value. To declare a variable, you type something like this:
var myCat;
This tells the JavaScript interpreter “Yo, here comes a variable, and name it
myCat, will you?”
Initializing a variable means setting a variable equal to some value, which you typically do at the same time you declare the variable. Here’s how you might initialize the variable myCat:
var myCat = "Fluffy"
Technically, you can declare a variable in JavaScript without using the var keyword, like so: myCat = "Fluffy". However, using the var keyword to declare all your variables is a good idea because it helps the JavaScript interpreter properly scope variables with the same name.
Chapter 3: JavaScript Language Basics
As of this writing, the next version of JavaScript, version 2.0 — due to be finalized later this year and (with luck) supported by upcoming browser tH У Ш 1 versions — provides for the strongly typed variables with which C and C+ +
programmers are familiar. What this means to you is that when browsers support JavaScript 2.0, you may use variable descriptors such as integer and number to declare upfront precisely what kind of value you want each variable to contain. Until then, however, no variable descriptors are necessary.
After you declare a variable — whether you use the var keyword or not — you can reset its value later in the script by using the assignment operator (=). The name of the variable can be any legal identifier (you want to use letters and numbers, not special characters), and the value can be any legal expression. (A legal expression is any properly punctuated expression that you see represented in this chapter: an if-else expression, an assignment expression, and so on.)
A variable is valid only when it’s in scope. When a variable is in scope, it’s been declared between the same curly brace boundaries as the statement that’s trying to access it. For example, if you define a variable named firstName inside a function called displayReport(), you can refer to the variable only inside the displayReport() function’s curly braces. If you try to access firstName inside another function, you get an error. If you want to reuse a variable among functions (shudder — that way lies madness), you can declare that variable near the top of your script before any functions are declared. That way, the variable’s scope is the entire script, and all the functions get to see it. Take a look at the following code example:
function displayReport() {
var firstName = document.myForm.givenName.value
alert(”Click OK to see the report for ” + firstName)
// Using firstName here is fine; it was declared // inside the same set of curly braces.
}
function displayGraph() {
alert('Here's the graph for ” + firstName) // Error!
// firstName wasn't defined inside this // function's curly braces!
}
As you can see from the comments in the this code fragment, it’s perfectly okay to use the firstName variable inside the displayReport() function because the firstName variable is in scope anywhere inside the
displayReport() function. It’s not okay, however, to use firstName inside displayGraph(). As far as displayGraph() is concerned, no such animal as firstName has been declared inside its scope!
58 Part I: Building Killer Web Pages for Fun and Profit
Literally speaking
Sometimes you want to use a number, a string, or some other value that you know for a fact will never change. For example, suppose that you want to write a script that uses pi in some calculation. Instead of creating a pi variable and assigning it the value of 1.31415, you can use the number 1.31415 directly in your calculations. Values that aren't stored in variables are called literals.
Previous << 1 .. 19 20 21 22 23 24 < 25 > 26 27 28 29 30 31 .. 118 >> Next