Creating dynamic classes

By default, the properties and methods of a class are fixed. That is, an instance of a class can't create or access properties or methods that weren't originally declared or defined by the class. For example, consider a Person class that defines two properties, userName and age.

To create a class that is not dynamic:

  1. Select File > New and then select ActionScript File, and then click OK.
  2. Type the following ActionScript into the Script window:
    class Person {
        public var userName:String;
        public var age:Number;
    }
    

    If, in another script, you create an instance of the Person class and try to access a property of the class that doesn't exist, the compiler generates an error.

  3. Save the file on your hard disk as Person.as.
  4. Select File > New and then select Flash Document to create a new FLA file, and then click OK.
  5. Select File > Save As, name the file person_test.fla, and save the file in the same directory as the Person class you created earlier.
  6. Add the following code to create a new instance of the Person class (firstPerson), and try to assign a value to a property called hairColor (which doesn't exist in the Person class):
    var firstPerson:Person = new Person();
    firstPerson.hairColor = "blue"; // Error. There is no property with the name 'hairColor'.
    
  7. Save the Flash document.
  8. Select Control > Test Movie to test the code.

    This code causes a compiler error because the Person class doesn't declare a property named hairColor. In most cases, this is exactly what you want to happen. Compiler errors might not seem desirable, but they are very beneficial to programmers: good error messages help you to write correct code by pointing out mistakes early in the coding process.

In some cases, however, you might want to add and access properties or methods of a class at runtime that aren't defined in the original class definition. The dynamic class modifier lets you do just that.

To create a dynamic class:

  1. Select File > New and then select ActionScript File, and then click OK.
  2. Select File > Save As and name the file Person2.as. Save the file on your hard disk.
  3. Type the following code into the Script window:
    dynamic class Person2 {
        public var userName:String;
        public var age:Number;
    }
    

    This ActionScript adds the dynamic keyword to the Person class in the previous example. Instances of the Person2 class can add and access properties and methods that are not defined in this class.

  4. Save your changes to the ActionScript file.
  5. Select File > New and then select Flash Document to create a new FLA file, and then click OK.
  6. Select File > Save As and name the new file person2_test.fla. Save it in the same directory as Person2.as.
  7. Type the following code to create a new instance of the Person2 class (firstPerson), and assign a value to a property called hairColor (which doesn't exist in the Person2 class).
    var firstPerson:Person2 = new Person2();
    firstPerson.hairColor = "blue";
    trace(firstPerson.hairColor); // blue
    
  8. Save your changes to the person2_test.fla file.
  9. Select Control > Test Movie to test the code.

    Because the custom Flash class is dynamic, you can add methods and properties to the class at runtime (when the SWF file plays). When you test the code the text blue should be displayed in the Output panel.

When you develop applications, you wouldn't want to make classes dynamic unless you needed to. One reason not to use dynamic classes is that type checking on dynamic classes is less strict than type checking on nondynamic classes, because members accessed inside the class definition and on class instances are not compared with those defined in the class scope. Class member functions, however, can still be type checked for return types and parameter types.

Subclasses of dynamic classes are also dynamic, with one exception. Subclasses of the MovieClip class are not dynamic by default, even though the MovieClip class itself is dynamic. This implementation provides you with more control over subclasses of the MovieClip class, because you can choose to make your subclasses dynamic or not:

class A extends MovieClip {}        // A is not dynamic
dynamic class B extends A {}        // B is dynamic
class C extends B {}                // C is dynamic
class D extends A {}                // D is not dynamic
dynamic class E extends MovieClip{} // E is dynamic

For information on subclasses, see Inheritance.