MACROMEDIA DIRECTOR MX 2004-DIRECTOR SCRIPTING Reference page 67

Director scripting reference
Table of Contents

Advertisement

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
expression.
new
Object instances
The most common way to create a new object instance is to use 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
When you create new object instances, the keyword
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
In the following example, a
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
// 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
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
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
You can also create an object instance by using "object literal" syntax, which eliminates the need
for the
operator and a constructor function. You typically only use this technique when you
new
need only one instance of an object that has not been defined in a constructor function. The
following example creates an object instance with
var objSmallCircle = { x:1, y:2, radius:2 };
undefined
constructor function uses the keyword
Circle
has been initialized, you can access its properties. Using the
50.
Object-oriented programming with JavaScript syntax
.
is used in the body of the associated
this
this.propertyName
objRectangle
,
, and
x = 1
y = 2
operator followed by the
new
syntax.
to specify the
this
.
this
objCircle
or
.
objCircle
.
radius = 2
67

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the DIRECTOR MX 2004-DIRECTOR SCRIPTING and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Director mx 2004

Table of Contents