| |
|
||||
|
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'.
Constructors < < Home > > __proto__
|