After you define the constructor function you must create an instance of the object. Use the
operator before the name of the constructor function and assign the new instance a variable name.
For example, the following code uses the
and assigns it to the variable
myCircle = new Circle(5);
Note: An object has the same scope as the variable to which it is assigned.
Assigning methods to a custom object in ActionScript 1
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, as in the following example, which creates the methods
function Circle(radius) {
this.radius = radius;
this.area = Math.PI * radius * radius;
this.diameter = function() {return 2 * this.radius;}
}
Each constructor function has a
define the function. The
created with that function. Each new instance of an object has a
to the
prototype
methods to an object's
that object. It's best to assign a method to the
because it exists in one place and is referenced by new instances of the object (or class). You can
use the
prototype
object-oriented manner. (For more information, see
on page
803.)
The following procedure shows how to assign an
To assign a method to a custom object:
Define the constructor function
1
function Circle(radius) {
this.radius = radius;
}
Define the
2
area()
circle. You can use a function literal to define the
to the circle's prototype object, as follows:
Circle.prototype.area = function () {
return Math.PI * this.radius * this.radius;
};
Create an instance of the Circle object, as follows:
3
var myCircle = new Circle(4);
Call the
4
area()
var myCircleArea = myCircle.area();
ActionScript searches the
have an
area()
ActionScript finds it and calls it.
:
myCircle
prototype
property indicates the default property values for objects
prototype
property of the constructor function that created it. Therefore, if you assign
property, they are available to any newly created instance of
prototype
and
properties to extend objects so that you can reuse code in an
__proto__
Circle()
method of the Circle object. The
method of the new
myCircle
object for the
myCircle
method, its prototype object
operator to create a Circle object with a radius of 5,
new
property that is created automatically when you
property of the constructor function
prototype
"Creating inheritance in ActionScript 1"
method to a custom Circle object.
area()
, as follows.
area()
method and assign the
area()
object, as follows:
method. Since the object doesn't
area()
Circle.prototype
and
area()
diameter()
property that refers
__proto__
method calculates the area of the
area
is searched for
area()
About ActionScript 1
new
:
property
.
801
Need help?
Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?
Questions and answers