User Guide
Object-oriented programming with JavaScript syntax 67
Constructor functions are typically used only to initialize new objects, but can also return the
object if desired. If you do return the initialized object, the returned object becomes the value of
the
new expression.
Object instances
The most common way to create a new object instance is to use the
new operator followed by the
name of a constructor function. The following examples create new object instances.
var objRandom = new Object(); // assigns a reference to an Object object
var objString = new String(); // assigns a reference to a String object
A constructor function can optionally define parameters that a new object instance passes to it to
initialize the state of the object instance. If a constructor function does define parameters used
during initialization of new object instances, the property values are initialized as follows:
• If you pass values to the constructor function during initialization, the properties that received
values are set to those values.
• If you do not pass values to the constructor function during initialization, the properties that
did not receive values are set to
undefined.
When you create new object instances, the keyword
this is used in the body of the associated
constructor function to refer to the new object instance. Therefore, a new object instance is
initialized with all of the properties defined by using the
this.propertyName syntax.
In the following example, a
Circle constructor function uses the keyword this to specify the
names of three properties that will be associated with new object instances. The statement
following the constructor initializes a new object instance by passing values to the constructor.
These values are used as the initial values of the properties specified by the keyword
this.
// Circle constructor function
function Circle(x, y, r) {
this.xCoord = x;
this.yCoord = y;
this.radius = r;
}
// xCoord = 10, yCoord = 15, radius = 5
var objCircle = new Circle(10, 15, 5);
Now that objCircle has been initialized, you can access its properties. Using the objCircle
instance created previously, you could set some variables equal to the values of its properties.
var theXCoord = objCircle.xCoord; // assigns the value 10 to theXCoord
var theYCoord = objCircle.yCoord; // assigns the value 15 to theYCoord
var theRadius = objCircle.radius; // assigns the value 5 to theRadius
Note: For more information on using dot syntax to access properties and methods of an object, see
“Scripting in dot syntax format” on page 50.
It is considered good scripting practice to give new objects names that map to their functionality,
and to name them by using lowercase letters, such as
objRectangle or objCircle.
You can also create an object instance by using "object literal" syntax, which eliminates the need
for the
new operator and a constructor function. You typically only use this technique when you
need only one instance of an object that has not been defined in a constructor function. The
following example creates an object instance with
x = 1, y = 2, and radius = 2.
var objSmallCircle = { x:1, y:2, radius:2 };