Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next



NOTE:  Although the words “function” and “method” are often used interchangeably, they are not the same. A method is a function that is part of an object. For instance, writeln is one of the methods of the object document.

Here’s a simple guideline: An object’s properties are the information it knows; its methods are how it can act on that information.

Using Built-In Objects and Functions Individual JavaScript elements are objects. String literals are string objects, for example, and they have methods that you can use to change their case, and so on. JavaScript can also use the objects that represent the Web browser in which it is executing, the currently displayed page, and other elements of the browsing session.

To access an object, you specify its name. Consider, for example, an active document object named document. To use document’s properties or methods, you add a period (.) and the name of the method or property you want. For example, document.title is the title property of the document object, and explorer.length calls the length member of the string object named explorer. Remember, literals are objects too.

You can find out more about the objects built in to JavaScript and the Web browser in the next chapter.

Using Properties Every object has properties, even literals. To access a property, use the object name followed by a period and the property name. To get the length of a string object named address, you can write the following:

address.length

You get back an integer that equals the number of characters in the string. If the object you use has properties that can be modified, you can change them in the same way. To set the color property of a house object, for example, use the following line:

house.color = “blue”

You can also create new properties for an object simply by naming them. If you define a class called customer for one of your pages, for example, you can add new properties to the customer object as follows:

customer.name = “Joe Smith”
customer.address = “123 Elm Street”
customer.zip = “90210”

Finally, knowing that an object’s methods are properties is important. You can easily add new methods to an object by writing your own function and creating a new object property using your own function name. If you want to add a Bill method to your customer object, you can do so by writing a function named BillCustomer and setting the object’s property as follows:

customer.Bill = BillCustomer;

To call the new method, you use the following:

customer.Bill()

Array and Object Properties JavaScript objects store their properties in an internal table that you can access in two ways. You have already seen the first way—just use the properties’ names. The second way, arrays, enables you to access all an object’s properties in sequence. The following function prints out all the properties of the specified object:

function DumpProperties(obj, obj_name) {
   result = “”    // set the result string to blank
   for (i in obj)
      result += obj_name + “.” + i + “ = “ + obj[i] + “\n”
   return result
}

You can access all the properties of the document object, for instance, both by property name—using the dot operator (for example, document.href)—and by the object’s property array (document[1], although this may not be the same property as document.href). JavaScript provides another method of array access that combines the two: associative arrays. An associative array associates a left- and a right-side element; the value of the right side can be used by specifying the value of the left side as the index. JavaScript sets up objects as associative arrays with the property names as the left side and their values as the right. You can, therefore, access the href property of the document object by using document[“href”].

Programming with JavaScript

JavaScript has a lot to offer page authors. It is not as flexible as C or C++, but it is quick and simple. Most importantly, it’s easily embedded in your Web pages; thus, you can maximize their impact with a little JavaScript seasoning. This section covers the gritty details of JavaScript programming and includes a detailed explanation of the language’s features.

Expressions

An expression is anything that can be evaluated to get a single value. Expressions can contain string or numeric literals, variables, operators, and other expressions, and they can range from simple to quite complex. The following are examples of expressions that use the assignment operator (more on operators in the next section) to assign numeric or string values to variables:

x = 7;
str = “Hello, World!”;

In contrast, the following is a more complex expression whose final value depends on the values of the quitFlag and formComplete variables:

(quitFlag == TRUE) & (formComplete == FALSE)

Operators

Operators do just what their name suggests: They operate on variables or literals. The items that an operator acts on are called its operands. Operators come in the following two types:

  Unary operators—These operators require only one operand, and the operator can come before or after the operand. The -- operator, which subtracts one from the operand, is a good example. Both --count and count-- subtract one from the variable count.
  Binary operators—These operators need two operands. The four math operators (+ for addition, - for subtraction, * for multiplication, and / for division) are all binary operators, as is the = assignment operator.

Assignment Operators Assignment operators take the result of an expression and assign it to a variable. JavaScript doesn’t enable you to assign the result of an expression to a literal. One feature of JavaScript not found in most other programming languages is that you can change a variable’s type on-the-fly. Consider the HTML document shown in Listing 18.1.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.