Sealed and dynamic classes

Back in the days of actionscript 2, you could add any property to pretty much any object, just calling it and setting a value, like this:

 myMovieClip.thePropertyILikeWithAStrangeName = "some string"

Actionscript 3.0 instead, being a more serious language, defines the basic class as sealed. That means that you can’t add any property on a object. You have to extends that class, and adding public properties.

public class MyClass extends MovieClip {
public thePropertyILikeWithAStrangeName:String
public function MyClass() {
super();
}
}

That class is sealed. You can’t

var foo:MyClass = new MyClass();
foo.anotherProperty = 42;

The dynamic keyword, permits to ‘unseal’ a class, giving back the possibility to add any property on runtime.

public dynamic class MyClass extends MovieClip {
public thePropertyILikeWithAStrangeName:String
public function MyClass() {
super();
}
}

Now you can:

var foo:MyClass = new MyClass();
foo.anotherProperty = 42;

There are speed and cleaness drawbacks on using dynamic classes. The rule is that if you use it, you should be able to seriously motivate it.

Note that there are few dynamic classes in the default Actionscript framework; the most known is of course, Object .

Read more:
livedocs
ActionScript 3: Dynamic Classes

Do you want to leave a comment?