appendix A

JavaScript Structure and Objects Reference


CONTENTS

This appendix is a quick reference for JavaScript objects. It includes the built-in objects and the objects in the object hierarchy. For further information, refer to Part II, "Using JavaScript Objects and Forms," of this book.

Built-In Objects

The following objects are built in to JavaScript. Some can be used to create objects of your own; others can be used only as they are. Each is summarized in the following sections. See Chapter 4, "Using Built-In Objects and Custom Objects," for details and examples.

Array

You can create a new Array object to define an array-a numbered list of variables. (Unlike other variables, arrays must be declared.) Use the new keyword to define an array, as in this example:

students = new Array(30)

Items in the array are indexed beginning with 0. Refer to items in the array with brackets:

fifth = students[4];

Arrays have a single property, length, which gives the current number of elements in the array. They have the following methods:

String

Any string of characters in JavaScript is a string object. The following statement assigns a variable to a string value:

text = "This is a test."

Because strings are objects, you can also create a new string with the new keyword:

text = new String("This is a test.");

string objects have a single property, length, which reflects the current length of the string. There are a variety of methods available to work with strings:

There are also a few methods that enable you to change a string's appearance when it appears in an HTML document:

As an example, this statement prints the value of the text string in italics:

document.write(text.italics());

Math

The Math object is not a "real" object, because you can't create your own objects. Each property or method uses the built-in Math object. A variety of mathematical constants are available as properties of the Math object:

The methods of the Math object enable you to perform mathematical functions. The methods are listed in the following sections in categories.

Algebraic Functions

Statistical and Logarithmic Functions

For example, this statement assigns the big variable to the larger of x and y:

big = Math.max(x,y);

Basic Math and Rounding

As an example, the following statement assigns the x variable to the square root
of 35:

x = Math.sqrt(25);

Random Numbers

Math.random() returns a random number between 0 and 1.

Note
The Math.random() method worked only on UNIX platforms until Netscape 3.0. Be sure you and your users use the latest version.

Date

The Date object is a built-in JavaScript object that enables you to work conveniently with dates and times. You can create a Date object any time you need to store a date and use the Date object's methods to work with the date:

Note
The Date object will not work with dates before January 1st, 1970.

navigator

The navigator object includes information about the current browser version. At present, it works only with Netscape browsers. Its properties include the following:

The JavaScript Object Hierarchy

The object hierarchy includes objects that represent the browser window, the current document, and its contents. These objects are summarized here and explained in detail in Chapter 5 "Accessing Window Elements as Objects."

window

The window object represents the current browser window. If multiple windows are open or frames are used, there may be more than one window object. These are given aliases to distinguish them:

Each window object includes the following properties:

The window object also has three child objects, which you'll look at in their own sections later:

The window object includes the following methods:

Finally, window objects have the following event handlers, which you can define in the document's <BODY> or <FRAMESET> tag:

location

The location object contains information about the current URL being displayed by the window. It has a set of properties to hold the different components of the URL:

The location object also has two methods:

history

The history object holds information about the URLs that have been visited before and after the current one in the window, and it includes methods to go to previous or next locations:

document

The document object represents the current document in the window and is a child of the window object. It includes the following properties:

The document object also includes the following child objects as properties:

The document object has no event handlers. It includes the following methods:

Creating and Customizing Objects

This is a brief summary of the keywords you can use to create your own objects and customize existing objects. These are documented in detail in Chapter 4 "Using Built-In Objects and Custom Objects."

Creating Objects

There are three JavaScript keywords used to create and refer to objects:

To create a new object, you need an object constructor function. This simply assigns values to the object's properties using this:

function Name(first,last) {
   this.first = first;
   this.last = last;
}

You can then create a new object using new:

Fred = new Name("Fred","Smith");

You can also create a generic object using the Object() constructor and define its properties later:

values = new Object();

Customizing Objects

You can add additional properties to an object you have created just by assigning them:

Fred.middle = "Clarence";

Properties you add this way apply only to that instance of the object, not to all objects of the type. A more permanent approach is to use the prototype keyword, which adds a property to an object's prototype (definition). This means that any future object of the type will include this property. You can include a default value:

Name.prototype.title = "Citizen";

You can use this technique to add properties to the definitions of built-in objects as well. For example, this statement adds a property called num to all existing and future string objects, with a default value of 10:

string.prototype.num = 10;