please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 7
Objects
Next
 

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 =index.html 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 =index.html 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 =index.html 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 ==index.html "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

file: /Techref/language/java/script/definitive/ch07_02.htm, 7KB, , updated: 2019/10/14 15:00, local time: 2024/11/20 02:41,
TOP NEW HELP FIND: 
18.223.125.42:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.org/techref/language/java/script/definitive/ch07_02.htm"> [Chapter 7] 7.2 Creating New Objects with Constructors</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .