Understanding Methods in Real-World Coding

 <  Day Day Up  >  

Understanding Methods in Real-World Coding

A method is an action that can be performed on or by an object. For example, with a book, we could have methods on the object such as read , write , and edit . A method is aware of all the public and private properties of the object and because of this, can easily work with that object using a method. For example, a read method of a book object would know the properties authors , chapters , and publisher .

Assuming that we had a class of Book , and an object derived from that class called oopAs that contained properties of publisher , totalChapter , and currentPage , you could go to Table 4.1 for a list of the methods of that class.

Table 4.1. Methods of This Book




New Riders




currentPage +1

In the following subsections, the text contains further details about methods.

ActionScript 2.0 Functions Versus ActionScript 2.0 Methods

All methods in ActionScript 2.0 are defined with the function keyword. However, there are very few real, free-standing functions in ActionScript 2.0. Most functions are methods of an object. For example, the getURL () function is actually a method of the MovieClip class. This function works fine if referenced from a MovieClip object (such as the main timeline), but will return an error of undefined if used within a class that has no relation to the MovieClip class, as shown in Listing 4.1:

Listing 4.1. getURL Is Available to Use on the Timeline
 function getHomeAdvisor() :Void {  this.getURL (http://www.homeadvisor.com, "_parent"); //opens up URL in external browser window } this.getHomeAdvisor(); 

In Listing 4.2, the function from Listing 4.1 is used within the Loan class, which, in this case, has nothing to do with a movie clip. Flash states that getURL is undefined, as shown in Figure 4.2.

Listing 4.2. The Function from Listing 4.1 Fails Here
 class Loan {       function Loan() {} //constructor       function getHomeAdvisor(){       this.getURL ("http://www.homeadvisor.com)", "_parent" )//results in an undefined error       } } 
Figure 4.2. Undefined getURL .


There are some functions in Macromedia Flash MX 2004, however, that are truly global, stand-alone functions. Examples include getTimer() and setInterval() . These functions can be used anywhere and objects do not have to be instantiated to use them, as shown in the following code:

 {       function Loan () {} //constructor       function getElaspedTime () {       return getTimer() //returns the number of milliseconds elapsed since the SWF was loaded } } 

Here is a list of some true functions (not methods) within ActionScript 2.0:

  • getTimer()

  • setInterval()

  • clearInterval()

  • getVersion()

  • isNan()

  • MMexecute()

  • Number()

  • eval()

  • escape()

  • unescape ()

  • trace()

User-Defined Functions to Methods

Whenever you add a function to the main timeline, you are creating a method attached to that timeline. For example, consider the following code attached to the main timeline:

 var name :String = "James"; this.getName = function () { return this.name; } trace (getName()); 

The output of "James" would result from the preceding code. getName() is now a method that is attached to only one object, not a class. In this case, the object is referenced by the this keyword. When the preceding code is placed on the main timeline, the this keyword refers to the main timeline.

Note that the scope of a method is affected by the order of the code; for example, the following code would result in an error of undefined because getName() has not yet been defined:

 var name :String = "James"; trace (getName()); this.getName = function () { return this.name; } 

To avoid these errors of scope, you should define methods using the function keyword. Using the function keyword, scope is not an issue. The following code would return "James":

 var name :String = "James"; trace (getName()); function getName () { return this.name; } 

You should get in the habit of defining all methods using the function keyword. Reason? Doing it the other way is not permitted in a class definition. For example, the following class definition results in an error:

 class Loan {       var interestRate = 5.1;       function Loan () {} //constructor       this.getInterestRate = function () {             return interestRate;       } } 

The preceding code would produce the error in Figure 4.3.

Figure 4.3. Statement not permitted in a class definition.


Functions within classes must be defined, as shown in the following code:

 class Loan {       var interestRate = 5.1;       function Loan () {} //constructor       function getInterestRate () {             return interestRate;       } } 


From this point on, all method definitions in this book will use the preceding notation.

Note that this book focuses on class-based development. In OOP, you should not assign functions to an individual object, but instead to a class. Generally, it is a bad practice to create methods that are attached to only one object. Following these best practices, this book attaches code directly to classes and uses only the timeline to instantiate those classes.

T ip

Understanding methods is integral to class based development. If we just create unique movie clips, as opposed to creating classes, distribution becomes difficult. It is much easier to distribute class files to other developers than it is to extract all the code from an individual movie clip in a FLA file. It also results in more maintainable and scaleable applications because it is much easier to make global changes in a movie clip.

Strong Datatypes for Methods

All methods (when they are declared) should specify the type of data they are returning. For example, in the following code, the getName() method is indicating that a string will be returned:

 var name :String = "James"; trace (getName()); function getName () :String { return this.name; } 

Flash MX 2004 has added a new datatype/returntype of Void . When you have a method that does not return any data, it is a best practice to always specify that the method does not return anything by using the Void keyword, as shown in the following code:

 class Loan { var interestRate = 5.1; function Loan (){} function getInterestRate() :String {       return interestRate; } function setInterestRate(interestRate:String) :Void {       this.interestRate = interestRate; } } 
 <  Day Day Up  >  

Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net