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)); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access