Sealed and dynamic classes
- Posted by kajyr on December 10th, 2009
- Comment now »
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 thePropertyILikeWithAStrangeName:String
public function MyClass() {
super();
}
}
That class is sealed. You can’t
foo.anotherProperty = 42;
The dynamic keyword, permits to ‘unseal’ a class, giving back the possibility to add any property on runtime.
public thePropertyILikeWithAStrangeName:String
public function MyClass() {
super();
}
}
Now you can:
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