User Guide

72 Chapter 3: Writing Scripts in Director
Deleting variables
You can delete a class variable or an instance variable by using the
delete operator. The following
example illustrates this process.
function Car() { // define a Car constructor function
...
}
Car.color = "blue"; // define a color property for the Car class
Car.prototype.engine = "V8"; // define an engine property for the prototype
var objCar = new Car();
trace(Car.color); // displays "blue"
trace(objCar.engine); // displays "V8"
delete Car.color;
delete Car.prototype.engine;
trace(Car.color); // displays undefined
trace(objCar.engine); // displays undefined
Accessing the constructor property of a prototype object
When you define a class by creating a constructor function, JavaScript syntax creates a prototype
object for that class. When the prototype object is created, it initially includes a
constructor
property that refers to the constructor function itself. You can use the
constructor property of a
prototype object to determine the type of any given object.
In the following example, the
constructor property contains a reference to the constructor
function used to the create the object instance. The value of the
constructor property is actually
a reference to the constructor itself and not a string that contains the constructor’s name.
function Car() { // define a Car class
// initialization code here
}
var myCar = new Car(); // myCar.constructor == function Car() {}
Creating properties dynamically
Another advantage of using prototype objects to implement inheritance is that properties and
methods that are added to a prototype object are automatically available to object instances. This
is true even if an object instance was created before the properties or methods were added.
In the following example, the
color property is added to the prototype object of a Car class after
an object instance of Car has already been created.
function Car(make, model) { // define a Car class
this.make = make;
this.model = model;
}
var myCar = new Car("Subaru", "Forester"); // create an object instance
trace(myCar.color); // returns undefined
// add the color property to the Car class after myCar was initialized
Car.prototype.color = "blue";
trace(myCar.color); // returns "blue"