Introduction
Objects
Instances
Local
Inheritance
Prototypes
Override
Overwrite
Protection
Arguments
Constructors
Methods
Proto
Arguments Array
Callee
New
Extend
Super
Glossary
 
 
Debreuil Digital Works © 2001
 

Methods and Properties

So far the properties in all of our examples have been simple values like 'rover' or 5 - or have they? The class name itself is assigned to a property (called Dog for example), and the prototype object seems to be a property of the class (we can say Dog.prototype after all). We can, in fact, assign pretty well anything to a property in Actionscript. This is quite different than many languages (Java, for example), so we are going to have to clarify what we mean by 'property'. In Java or C++, there is a very clear distinction between 'methods' and 'properties'; in Actionscript there isn't. So what are methods?

Methods are similar to what you might normally think of as functions (as in routines), however they are always associated with a class, and generally called from an instance. They are used to modify an object and/or return a computed value. They are not used to create a new instance. Methods never utilize their prototype, and they never put properties in their namespace. They are not anything like classes. They are routines that belong to the class.

That being said, methods do 'look' similar to classes (Ed. cough cough, ahem.). They are also created using a function, however they are never called using the 'new' operator. Their 'this' keyword refers to the object that called them, rather than a new object being created. Because they do not automatically return a new object like classes do, methods can - and arguably should return a value - even if it's void (though this is not a requirement, or even a common practice - it is a way of life in stricter languages though, so perhaps a good habit. Or not). Lastly, like other properties, to help tell them apart from classes methods should always be defined with a lowercase initial letter (some prefer to capitalize methods, and only make properties lower case - your choice, but you should at least be consistent). Let's look at a quick example of how methods are created and called - then we'll try to sort out all these terms that may be starting to blur.

// Dog Class
Dog = function( age )
{
  this.age = age;
  this.dogYears = this.calcDogYears( this.age );
}
// Dog method - Calculate age in dog years
Dog.prototype.calcDogYears = function( age )
{
  return ( this.age * 6 );
}
// Dog method - Dog has birthday
Dog.prototype.haveBirthday = function( )
{
  this.age++;
  this.dogYears = this.calcDogYears( this.age );
  trace("Happy Birthday!")
}

// make an instance of Dog
yeller = new Dog( 12 );
trace( yeller.dogYears ); // 72
yeller.haveBirthday( );   // method traces "Happy Birthday!"
trace( yeller.dogYears ); // 78

The Dog methods are called in three ways in this program - can you spot them? 'calcDogYears' is called twice - once from the Dog constructor, and once from within a different method in the prototype. 'haveBirthday' is called once, directly from the instance. In methods, the 'this' keyword refers to the object that is calling the method, so methods can be used to modify (add, delete, change) properties in the object calling them. All properties that are available to the object that calls a method are available to the method as well, using the this keyword. Of course that includes other prototype methods (as we can see inside the haveBirthday method).

Can you see why you always have to define the prototypes before you create the instances? Flash can not call methods that have not yet been defined (known as forward referencing) because Flash interprets and runs code as it comes to it (and as swf files stream, you can't even be sure code ahead of the current code is even loaded). If you look at the class constructor above, it would seem to contradict this. The Method calcDogYears is called from the constructor block, before it is defined in the prototypes. However, the constructor does not run until it is called - this happens when the instance 'yeller' is created. When writing code, you should consider your Class and its prototype to be a single block, and define them together. Generally prototype methods get defined after the prototype properties.

Though it isn't unheard of to put methods in the instance (e.g. the first sheet of glass), it is uncommon. Methods by nature are much more static than things like stings or numbers, so they are most likely going to be the same for every instance created from the class. Sometimes you may want to set methods at run time, by passing them as arguments to a constructor. This is the type of situation where you would end up seeing methods in an instance. If you find yourself defining methods in the constructor with a series of if-else statements, it's probably time for a subclass. We will cover this shortly.

Function vs Class vs Constructor vs Method vs Property... Ugh!

Ok, like it or not, we are going to have to clarify some terms here (otherwise how shall we speak?). In Actionscript almost everything is both a property and an object. This is a great thing, but it therefore becomes very important then to define things not just by what they physically are, but also by what you intend them to do. With this in mind let's start with 'class', 'method', and 'function'.

 

More Definitions

  • Class - This is the definition or template used to make new objects. Keyword: new. It includes the prototype definitions for the class. It does not have a return value, because it returns the new object that it created. It is always invoked with the 'new' operator, and each time it's called it creates a new instance. The 'this' keyword refers to the new instance being created. Arguments are used to give each instance customized properties.
  • Method - this is a routine, almost always defined in a class prototype, and always invoked through an object (most often from an instance that has direct access to the method). It can be passed arguments and it can return a value. The 'this' keyword refers to the object that invoked it. Though methods have a prototype and namespace, these should be ignored.
  • Function - You may wonder where 'normal' functions fit in here - ones that don't create new objects and don't belong to any class or instance. Well, they don't. If you are going to use OO programming, then you pretty much have to embrace it completely. Functions that sit out there on their own are from that other world called procedural programming (ah yes, and they are cool and useful, just not in OO).
  • Constructor - refers to the class that creates/created an object. It points exactly to the class name object, which has the block of code known as the activation object tied tightly below it.
  • Properties - Technically, anything assigned to an object becomes that object's property. So, this includes strings, numbers, etc., but also methods, and other objects... The term 'properties' often suggests all 'non-methods', so be aware (beware, be wary) of this. Actionscript makes no distiction between methods and 'properties', but you will often want to. Properties are also sometimes called attributes (data only), variables (data only), members (data or methods), or even fields (data only - in C#). Whatever your term, all of these will have the same double meaning problem once you start using them (if you clarify what you mean by a property - that holds only data - you are still able to assign a method to it in Actionscript).. Happily, 99% of the time you only need the context to understand the meaning. As for the other 1%, well, it doesn't really matter what was meant anyway - and that's the beauty of Actionscript. So to recap, for our purposes here, a property means all non-methods, or both methods and non-methods, depending on the context. Is that a laser precision defintion or what!
  • Members - This term is probably worth mentioning again, as it is a fairly common way of referring to both 'properties' and methods in an object. A class or object is like a set; the things in it are members of the set.
  • Object vs. Instance - These are the same thing, although 'instance' suggests an object that you created using 'new', while 'object' would suggest all objects. Strings and numbers are certainly objects, though in fact they are also instances, created with an internal string or number constructor. Usually the word instance does not refer to these types of objects, but again, you can usually tell from the context. There, that should fog things up nicely for you.

Constructors < < Home > > __proto__