AS1 OOP: Custom Object Classes
         by senocular  

Class Constructors
Class instances in Actionscript 1.0 are defined with a constructor function. Constructor functions are functions that are used to create or construct an instance of your new custom object. They define what properties and what methods each instance of the class will have when created.

To the naked eye, constructor functions look just like any other function. In fact, there is no real technical difference between the constructor function of a class and any other function. The difference comes only in its use. With that, keep in mind that the same rules for functions apply. If you define a function in _root and decide to use that function in some timeline other than _root, you would need to reference that function through _root. It’s not readily available for use in all timelines, just the timeline it was defined. If you do want the function, or class constructor in our case, to be globally available everywhere, you might consider defining it in _global.

Here is a simple example of a House class constructor function.

House = function(){
this.floors = 4;
this.siding = "Red";
};

Because it will be used as a constructor function and not a normal function, what we have here is the beginnings of the House class definition. This House class will then be used to make custom House object instances. Each custom House object made with this class constructor will have a floors and a siding property associated with it. By default, floors will have a value of 4 and siding, "Red."

When the House constructor is called using the new keyword, a new object is created - a new House instance. It’s the new keyword that makes the use of House a constructor and not just another function.

// House class
House = function(){
this.floors = 4;
this.siding = "Red";
};

 
// create an instance of the House class
myHouse = new House();

 
// check to see if the properties correctly exist
trace(myHouse.floors); // traces 4
trace(myHouse.siding); // traces "Red"

 

Prev Page
 



SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.