AS1 OOP: Custom Object Classes
by senocular
Defining Classes
Lets get back to defining the House class. Starting with the
class constructor we have the following.
- House = function(){
- this.floors = 4;
- this.siding = "Red";
- };
This is the definition for creating house instances
which, by default, will each be created with two properties,
floors and siding with the values 4 and “Red” respectively.
As classes go, this one’s fairly simple. In fact it’s simply
constructor for creating instances that serve as containers
for 2 pieces of information. Some functionality can be added
to house instances by adding a simple method in the function
constructor. Then, each house instance will have access to
this method much like each array has access to its method
push.
- House = function(){
- this.floors = 4;
- this.siding = "Red";
- this.outputSiding = function(){
- trace(this.siding);
- };
- };
- // create an instance of the House class
- myHouse = new House();
- myHouse.outputSiding(); // traces "Red"
In these kinds of methods, when defined in the
constructor like this (which isn’t common – something to be
discussed later), be sure to assign the function to the this
reference as this is the object being created. That is,
after all, the object you want to use the method with.
Inside the method function definition, this will also still
correctly reference the new object instance as the this
object is the object doing the calling of the method.
Like any other function, class constructors can also take
function parameters so that you can send values into the
constructor call helping you uniquely define each instance
created. For example, we can make the siding of the house
class customizable by passing it in as a parameter in the
function call.
- // class definition
- House = function(siding){
- this.floors = 4;
- this.siding = siding;
- this.outputSiding = function(){
- trace(this.siding);
- };
- };
- // create an instance of the House class to have blue
siding
- myHouse = new House("Blue");
Here, myHouse has a siding value of "Blue." Note that
Flash is able to distinguish the difference of the
instance’s siding variable and the passed siding variable
through the use of this. The this keyword indicates the
difference between values in the created object and values
from the local scope of the function (or the timeline even).
Here, siding was used for both the argument variable and and
instance’s property though it is not necessary. Feel free to
use separate variable names for each if you please.
If now, say you want the siding to be optional, you could
also allow for that in the constructor function definition.
You would just have to check to see if a siding value was
passed. If it was passed, assign the siding value to that
passed siding, otherwise use a default as defined by you.
This version of the House class assigns the siding of the
new House instance to be the passed siding value if it was
passed and is not undefined. Otherwise, it sets the siding
to be "Red."
- // class definition
- House = function(siding){
- this.floors = 4;
- if (siding != undefined){
- this.siding = siding;
- }else{
- this.siding = "Red";
- }
- this.outputSiding = function(){
- trace(this.siding);
- };
- };
- // create an instance of the House class to have blue
siding
- myHouse = new House("Blue");
- // create an instance of the House class to have red
siding
- myHouse = new House();
Don't, however, feel obligated to make all properties
used in your constructors optional this way. Often these
passed values are required. Besides, it's less work on you
having to check to see whether they're defined or not to see
whether they need to be assigned which value, and who needs
that? For the House class constructor, we can leave siding
optional, but for the floors property, we can make that a
required parameter when creating a new house instance. Not
every (if any) house made needs to be a whopping 4 stories
high. Users of the House constructor will just need to know
to make sure they specify how many floors they want their
house to be when they make a new House.
- // revised class definition
- House = function(floors, siding){
- this.floors = floors;
- if (siding != undefined){
- this.siding = siding;
- }else{
- this.siding = "Red";
- }
- this.outputSiding = function(){
- trace(this.siding);
- };
- };
And with that you have a fairly complete, though simple,
class definition complete with properties and a method which
will be copied into each instance of a house created.