The following example defines a function named
then assigned to the
// increase the speed of a Car
function Car_increaseSpeed(x) {
this.speed += x;
return this.speed;
}
Car.prototype.increaseSpeed = Car_increaseSpeed;
An object instance of
a variable by using the following syntax.
var objCar = new Car("Subaru", "Forester", "silver");
var newSpeed = objCar.increaseSpeed(30);
You can also create an instance method by using function literal syntax. Using function literal
syntax eliminates the need to define a function, and the need to assign a property name to the
function name.
The following example uses function literal syntax to define an
contains the same functionality as the
// increase the speed of a Car
Car.prototype.increaseSpeed = function(x) {
this.speed += x;
return this.speed;
}
Class variables
Also known as static variables, these are any variables (properties) that are associated with a class,
and not an object instance. There is always only one copy of a class variable, regardless of the
number of object instances that are created from that class. Class variables do not use the
prototype object to implement inheritance. You access a class variable directly through the class,
and not through an object instance; you must define a class in a constructor function before you
can define class variables.
The following example defines two class variables—
function Car() { // define a Car class
...
}
Car.MAX_SPEED = 165;
Car.MIN_SPEED = 45;
You would access the
var carMaxSpeed = Car.MAX_SPEED; // carMaxSpeed = 165
var carMinSpeed = Car.MIN_SPEED; // carMinSpeed = 45
Class methods
Also known as static methods, these are any methods that are associated with a class, and not an
object instance. There is always only one copy of a class method, regardless of the number of
object instances that are created from that class. Class methods do not use the prototype object to
implement inheritance. You access a class method directly through the class, and not through
an object instance; you must define a class in a constructor function before you can define
class methods.
70
Chapter 3: Writing Scripts in Director
property of the
increaseSpeed
could then access the
Car
increaseSpeed()
and
MAX_SPEED
MIN_SPEED
Car_increaseSpeed()
class's prototype object.
Car
method and assign its value to
increaseSpeed()
increaseSpeed()
function defined previously.
and
MAX_SPEED
MIN_SPEED
class variables directly from the
. The function name is
method that
.
class.
Car
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