
Object-oriented programming with JavaScript syntax
69
Prototype objects typically are not suited to define properties and methods whose values may vary
across object instances. In cases where values may vary across object instances, you typically define
those properties and methods within the class itself.
To specify the scope of a custom property or method, you define it as one of the following
four types:
•
Instance variables
•
Instance methods
•
Class variables
•
Class methods
Instance variables
Instance variables are any variables (properties) that are defined in a constructor function and that
are copied into each object instance of that constructor. All object instances have their own copies
of instance variables. This means that if there are five object instances of a
Circle
class, there are
five copies of each instance variable defined in the class. Because each object instance has its own
copy of an instance variable, each object instance can assign a unique value to an instance variable
without modifying the values of other copies of the instance variable. You access instance variables
directly from their containing object instances.
The following example defines four instance variables—
make
,
model
,
color
, and
speed
—in a
constructor function. These four instance variables are available directly from all object instances
of the
Car
constructor.
function Car(make, model, color) { // define a Car class
this.make = make;
this.model = model;
this.color = color;
this.speed = 0;
}
The following object instance
objCar
contains all four instance variables. Although a value for
the instance variable
speed
is not passed to the
Car
constructor,
objCar
still has a
speed
property
whose initial value is 0 because the
speed
variable is defined in the
Car
constructor.
// objCar.make="Subaru", objCar.model="Forester",
// objCar.color="silver", objCar.speed = 0
var objCar = new Car("Subaru", "Forester", "silver");
Instance methods
Instance methods are any methods that are accessible through an object instance. Object instances
do not have their own copies of instance methods. Instead, instance methods are first defined as
functions, and then properties of the constructor function’s prototype object are set to the
function values. Instance methods use the keyword
this
in the body of the defining constructor
function to refer to the object instance they are operating on. Although a given object instance
does not have a copy of an instance method, you still access instance methods directly from their
associated object instances.
Содержание DIRECTOR MX 2004-DIRECTOR SCRIPTING
Страница 1: ...DIRECTOR MX 2004 Director Scripting Reference...
Страница 48: ...48 Chapter 2 Director Scripting Essentials...
Страница 100: ...100 Chapter 4 Debugging Scripts in Director...
Страница 118: ...118 Chapter 5 Director Core Objects...
Страница 594: ...594 Chapter 12 Methods...
Страница 684: ...684 Chapter 14 Properties See also DVD...
Страница 702: ...702 Chapter 14 Properties See also face vertices vertices flat...
Страница 856: ...856 Chapter 14 Properties JavaScript syntax sprite 15 member member 3 4...
Страница 889: ...netPresent 889 _player alert Sorry the Network Support Xtras could not be found See also Player...
Страница 1102: ...1102 Chapter 14 Properties...