68
Chapter 3: Writing Scripts in Director
Object inheritance
In addition to being able to create your own custom classes, another major advantage of object-
oriented programming is the ability of subclasses to inherit the properties and methods of the
superclasses from which they were created. Inheritance enables you to easily create objects that
already have built-in properties and functionality.
In JavaScript syntax, there is one superclass that acts as the base class from which all other
subclasses are created—the Object superclass. The Object superclass contains a few basic
properties and methods. The subclasses that are created by using Object as a template always
inherit these basic properties and methods, and likely define their own properties and methods.
Subclasses of these classes inherit from Object, from their superclasses, and so on. All additional
objects that you create would continue this chain of inheritance.
For example, Object contains the
constructor
property and the
toString()
method. If you
create a new class named
SubObj1
, it is a subclass of Object, and therefore automatically inherits
the
constructor
property and the
toString()
method of Object. If you then create another
class named
SubObj2
using
SubObj1
as a superclass,
SubObj2
would also inherit the
constructor
property and the
toString()
method of Object, in addition to any custom
properties and methods you defined in
SubObj1
.
Two of the important properties that your custom constructor functions inherit from the Object
superclass are
prototype
and
constructor
. The
prototype
property represents the
prototype
object
of a class, which enables you to add variables (properties) and methods to object instances,
and is the means by which inheritance is typically implemented in JavaScript syntax. The
constructor
property represents the constructor function itself. The use of these properties is
explained in the following sections.
Prototype objects
As previously stated, when you create a subclass, it automatically inherits the properties and
methods of the superclass on which it is based. In JavaScript syntax, inheritance is typically
implemented by using prototype objects. A subclass actually inherits its properties and methods
from the prototype object of its superclass, and not from the superclass itself. This important
point offers a distinct advantage: all properties and methods do not literally have to be copied
from a class to an object instance of that class, which can dramatically decrease the amount of
memory required by new object instances.
Every class in JavaScript syntax, including the predefined Object class, contains only one
prototype object. Every object instance created from a class has access to the properties and
methods in the prototype object of that class. Therefore, the prototype object of a class is typically
the only object that actually stores the properties and methods for that class; an object instance
only contains the properties required to initialize that instance.
In your code, it appears that each object instance actually contains those properties and methods
because you can access them directly from each object instance, but the instance is actually using
the prototype object to access them. The prototype object of a class is created automatically when
you create the class. You access the prototype object by using the
prototype
property of the class.
Because a prototype object of a class stores properties that are shared by all object instances, they
are ideally suited to define properties and methods whose values will be shared across all object
instances. By sharing properties and methods across object instances, you can easily create
instances that exhibit a defined default behavior, and can also customize any instances that deviate
from the default behavior.
Summary of Contents for DIRECTOR MX 2004-DIRECTOR SCRIPTING
Page 1: ...DIRECTOR MX 2004 Director Scripting Reference...
Page 48: ...48 Chapter 2 Director Scripting Essentials...
Page 100: ...100 Chapter 4 Debugging Scripts in Director...
Page 118: ...118 Chapter 5 Director Core Objects...
Page 594: ...594 Chapter 12 Methods...
Page 684: ...684 Chapter 14 Properties See also DVD...
Page 702: ...702 Chapter 14 Properties See also face vertices vertices flat...
Page 856: ...856 Chapter 14 Properties JavaScript syntax sprite 15 member member 3 4...
Page 1102: ...1102 Chapter 14 Properties...