O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

12.1. Using Methods and Properties of Built-in Objects

Problem

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

Solution

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

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)); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required