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

JavaScript: The Definitive Guide

Previous Chapter 7

7.2 Creating New Objects with Constructors

As we saw briefly in Chapter 4, Expressions and Operators, the new operator creates a new object. For example:

o = new Object();

This syntax creates an "empty" object, one that has no properties defined. There are certain occasions in which you might want to start with an empty object of this sort, but in "object-oriented" programming, it is more common to work with objects that have a predefined set of properties. For example, you might want to define one or more objects that represent rectangles. In this case, each rectangle object should have a width property and a height property.

To create objects with properties such as width and height already defined, we need to write a constructor to create and initialize these properties in a new object. A constructor is a JavaScript function with three special features:

Example 7.1 shows how the constructor function for a rectangle object might be defined and invoked.

Example 7.1: A Rectangle Object Constructor Function

// define the constructor.
// Note how it initializes the object referred to by "this"
function Rectangle(w, h) 
    this.width = w;
    this.height = h;
// invoke the constructor to create two rectangle objects
// Notice that we pass the width and height to the constructor, so it
// can initialize each new object appropriately.
rect1 = new Rectangle(2, 4);
rect2 = new Rectangle(8.5, 11);

Notice how the constructor performs its initialization on the object referred to by the this keyword. A constructor will generally perform initialization based on the argument values that are passed to it. Some constructors may also initialize other properties of a new object (setting them to constant values, for example). Keep in mind that a constructor function simply initializes the specified object; it does not have to return that object.

Also notice how we define a "class" of objects simply by defining an appropriate constructor function--all objects created with that constructor will have the same properties. It is stylistically important to give constructor functions a name that indicates the class of objects they will "construct." For example, creating a rectangle with new construct_rect(1,2) is a lot less intuitive than new Rectangle(1,2).

The constructor Property

In Navigator 3.0, but not in Internet Explorer 3.0, all objects have a constructor property that refers to the constructor function that was used to create the object. Since the constructor function determines the "class" of an object, the constructor property in a sense specifies the "type" of any given object. For example, you might use code like the following to determine the type of an unknown object:

if ((typeof n == "object") && (n.constructor == Number))
   // then do something with the Number object...

Previous Home Next
Object Properties Book Index Methods

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