Полезная информация

JavaScript: The Definitive Guide

Previous Chapter 7 Next

7. Objects

Object Properties
Creating New Objects with Constructors
Object Prototypes
Classes in JavaScript
Objects as Associative Arrays
Special Object Methods

Chapter 3, Variables and Data Types, explained that objects are one of the fundamental data types in JavaScript. They are also one of the most important. This chapter describes JavaScript objects in detail. Basic usage of objects, described in the first section below, is straightforward, but as we'll see in later sections, objects have more complex uses and behaviors.

7.1 Object Properties

An object is a data type that contains named pieces of data. Each named datum is called a property. Each property has a name, and the object associates a value with each property name. A property value may be of any type. In effect, the properties of an object are variables within the "name space" created by the object.

Reading and Writing Object Properties

You normally use the . operator to access the value of an object's properties. The value on the left of the . should be a reference to an object (usually just the name of the variable that contains the object reference). The value on the right of the . should be the name of the property. This must be an identifier, not a string or an expression. For example, you refer to the property p in object o with o.p. Or, you refer to the property document in the object parent with parent.document. The . operator is used for both reading and writing object properties. For example:

// Read a property value:
w = image.width;
// Set a property value:
window.location = "http://my.isp.com/my_home_page/index.html";
// Read one property and set it in another property
image.src = parent.frames[1].location

Defining New Object Properties

You can add a new property to an object simply by setting its value. Thus, you might add a property to the object win with code like the following:

win.creator = self;
This line assigns the value of self to the creator property of the object win. If the creator property does not already exist, then it will be created so that the value can be assigned to it.

Undefined Object Properties

If you attempt to read the value of a property that does not exist--i.e., has never had a value assigned to it--you will retrieve the special JavaScript undefined value (which was introduced in Chapter 3, Variables and Data Types).

Once a property has been defined in an object, however, there is no way to undefine it. You may set the value of a property to the special undefined value, by assigning the value of an undefined property, but this just changes the value of the property without actually undefining it. You can demonstrate that the property still exists by using a for/in loop to print out the name of all defined properties:

for (prop in obj) 
    property_list += prop + "\n";

Previous Home Next
Event Handlers Book Index Creating New Objects with Constructors

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell