DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 12.1 Using Methods and Properties of Built-in Objects

12.1.1 Problem

You want to use the properties and methods of a built-in object, such as the Math object, to perform a given operation.

12.1.2 Solution

Access the properties and methods of the built-in object directly, such as Math.random( ) or Math.PI.

12.1.3 Discussion

ActionScript includes several objects with methods and properties that you can access directly from the class (there is no need to construct an instance of the class). Methods and properties accessed in this manner are often referred to as static methods and properties. Static methods and properties are often defined for classes that do not get instantiated. For example, the built-in Math, Key, Selection, and Stage objects define multiple static methods and properties. However, there are also classes, from which objects are otherwise instantiated, that include one or more static methods or properties. For example, the String class is typically instantiated (meaning you normally create string objects) but it also includes the static method String.fromCharCode( ). Likewise, the Object class is typically instantiated but it includes the static method Object.registerClass( ). In these cases, the static methods or properties are associated with the class because they don't pertain to a specific instance of the class.

In any case, you can access static methods and properties by invoking them directly from the class using dot syntax. Here are two examples:

trace(Math.PI);
trace(String.fromCharCode(108));

12.1.4 See Also

Recipe 12.2. Also refer to the "Language Reference" section of ActionScript for Flash MX: The Definitive Guide (O'Reilly), which documents ActionScript's Accessibility, Capabilities, Arguments, Key, Math, Mouse, Selection, SharedObject, Stage, and System objects in detail.

    [ Team LiB ] Previous Section Next Section