244 Classes
Using a class file in Flash
To create an instance of an ActionScript class, use the new operator to invoke the class’s
constructor function. The constructor function always has the same name as the class and
returns an instance of the class, which you typically assign to a variable. For example, if you
were using the User class from “Writing custom class files” on page 235, you would write the
following code to create a new User object:
var firstUser:User = new User();
Use the dot (.) operator to access the value of a property in an instance. Type the name of the
instance on the left side of the dot, and the name of the property on the right side. For
example, in the following statement,
firstUser is the instance and username is the property:
firstUser.username
You can also use the top-level or built-in classes that make up the ActionScript language in a
Flash document. For example, the following code creates a new Array object and then shows
its
length property:
var myArray:Array = new Array("apples", "oranges", "bananas");
trace(myArray.length); // 3
For more information on using custom classes in Flash, see “Example: Using custom class files
in Flash” on page 276. For information on the constructor function, see “Writing the
constructor function” on page 268.
NOTE
In some cases, you don’t need to create an instance of a class to use its properties and
methods. For more information on class (static) members, see “About class (static)
members” on page 298 and “Static methods and properties” on page 249.