Top Level

# Class - AS3

Package | Top Level
Class | public dynamic class Class
Inheritance | Class Object

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

A Class object is created for each class definition in a program. Every Class object is an instance of the Class class. The Class object contains the static properties and methods of the class. The class object creates instances of the class when invoked using the `new` operator.

Some methods, such as `flash.net.getClassByAlias()`, return an object of type Class. Other methods may have a parameter of type Class, such as `flash.net.registerClassAlias()`.

The class name is the reference to the Class object, as this example shows:

     class Foo {
     }

The `class Foo{}` statement is the class definition that creates the Class object Foo. Additionally, the statement `new Foo()` will create a new instance of class Foo, and the result will be of type Foo.

Use the `class` statement to declare your classes. Class objects are useful for advanced techniques, such as assigning classes to an existing instance object at runtime, as shown in the "Examples" section below.

Any static properties and methods of a class live on the class's Class object. Class, itself, declares `prototype`.

Generally, you do not need to declare or create variables of type Class manually. However, in the following code, a class is assigned as a public Class property `circleClass`, and you can refer to this Class property as a property of the main Library class:

Copy

     package {
      import flash.display.Sprite;
      public class Library extends Sprite {

          public var circleClass:Class = Circle;
          public function Library() {
          }
      }
     }

     import flash.display.Shape;
     class Circle extends Shape {
      public function Circle(color:uint = 0xFFCC00, radius:Number = 10) {
          graphics.beginFill(color);
          graphics.drawCircle(radius, radius, radius);
      }
     }

Another SWF file can load the resulting Library.swf file and then instantiate objects of type Circle. The following example shows one way to get access to a child SWF file's assets. (Other techniques include using `flash.utils.getDefnitionByName()` or importing stub definitions of the child SWF file.)

Copy

     package {
      import flash.display.Sprite;
      import flash.display.Shape;
      import flash.display.Loader;
      import flash.net.URLRequest;
      import flash.events.Event;
      public class LibaryLoader extends Sprite {
          public function LibaryLoader() {
              var ldr:Loader = new Loader();
              var urlReq:URLRequest = new URLRequest("Library.swf");
              ldr.load(urlReq);
              ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
          }
          private function loaded(event:Event):void {
              var library:Object = event.target.content;
              var circle:Shape = new library.circleClass();
              addChild(circle);
          }
      }
     }

In ActionScript 3.0, you can create embedded classes for external assets (such as images, sounds, or fonts) that are compiled into SWF files. In earlier versions of ActionScript, you associated those assets using a linkage ID with the `MovieClip.attachMovie()` method. In ActionScript 3.0, each embedded asset is represented by a unique embedded asset class. Therefore, you can use the `new` operator to instantiate the asset's associated class and call methods and properties on that asset.

View the examples

Related API Elements

Object.prototype
new operator

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | toLocaleString():String Returns the string representation of this object, formatted according to locale-specific conventions. | Object
|  | toString():String Returns the string representation of the specified object. | Object
|  | valueOf():Object Returns the primitive value of the specified object. | Object

Examples How to use this example
Class.1.as
The following example shows how to use Class objects to defer until runtime the decision about which class to instantiate using the following steps:

1. Declare two classes as `ClassA` and `ClassB`.
2. Declare a variable of type Class named `classToConstruct` and one of type Boolean `chooseClassA`, which is set to `true` in this case, but your code could use a custom test expression to set the value of that variable.

Copy

    package {
        import flash.display.Sprite;

        public class ClassExample extends Sprite {
            public function ClassExample() {
                var classToConstruct:Class;
                var classInstance:Object;

                classToConstruct = ClassA;
                classInstance = new classToConstruct();
                trace(classInstance);    // [object ClassA]

                classToConstruct = ClassB;
                classInstance = new classToConstruct();
                trace(classInstance);    // [object ClassB]
            }
        }
    }

    class ClassA {
    }

    class ClassB {
    }

© 2015 Adobe Systems Incorporated. All rights reserved.
Mon Apr 10 2017, 12:28 PM -07:00