|< Day Day Up >|
Methods , in practice, are no different than any other function, except that they are specifically bound to a particular object (or class, in the case of static methods). Methods are essential to performing an action on an object, and object methods are aware of the properties of that class.
ActionScript 2.0 is mostly composed of methods. If you place your code on the main timeline or on the timeline of a movie clip, you are basically attaching methods to a MovieClip object. This can be one of the most confusing concepts of ActionScript 2.0 because the main timeline is always instantiated . For example, getURL can be used on the main timeline but cannot be used within a class because it's a method of the MovieClip object. Whenever a function is defined on the main timeline, it's really a method of the main timeline MovieClip object. There are some freestanding built-in functions in ActionScript 2.0, such as getTimer() . These functions can be used anywhere .
If a method returns a datatype, it is a best practice to specify what datatype, such as String , Object or Array , is returned. If the method returns nothing, the datatype of Void should be specified.
The best practice in ActionScript 2.0 is to perform as much class-based development as possible. This means creating methods within classes, instead of creating methods attached to the timelines of objects. It is important to do this because it allows us to build much more scaleable and maintainable applications.
By creating methods within classes, we can specify whether they are public or private. If they are private, methods can be accessed only in the class (or any subclasses). In terms of OOP development, it is a best practice to always access variables through methods.
Note that static methods enable us to conserve resources and they cannot be data aware. In addition, methods are an integral part of classes and class-based development.
|< Day Day Up >|