Assigning methods to a custom object in
ActionScript 1.0
Many Flash users can greatly benefit from using ActionScript 2.0, especially with
complex applications. For information on using ActionScript 2.0, see
"Classes," on page
You can define the methods of an object inside the object's constructor function. However,
this technique is not recommended because it defines the method every time you use the
constructor function. The following example creates the methods
: and traces the area and diameter of the constructed instance
getDiameter()
a radius set to 55:
function Circle(radius) {
this.radius = radius;
this.getArea = function(){
return Math.PI * this.radius * this.radius;
};
this.getDiameter = function() {
return 2 * this.radius;
};
}
var myCircle = new Circle(55);
trace(myCircle.getArea());
trace(myCircle.getDiameter());
Each constructor function has a
define the function. The
created with that function. Each new instance of an object has a
refers to the
prototype
assign methods to an object's
instance of that object. It's best to assign a method to the
constructor function because it exists in one place and is referenced by new instances of the
object (or class). You can use the
that you can reuse code in an object-oriented manner. (For more information, see
inheritance in ActionScript 1.0" on page
The following procedure shows how to assign an
Circle object.
225.
property that is created automatically when you
prototype
property indicates the default property values for objects
prototype
property of the constructor function that created it. Therefore, if you
property, they are available to any newly created
prototype
and
prototype
798.)
Assigning methods to a custom object in ActionScript 1.0
getArea()
__proto__
property of the
prototype
properties to extend objects so
__proto__
method to a custom
getArea()
Chapter 7,
and
with
myCircle
property that
"Creating
795
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?