|
|
||||||||
| Ultrashock Tutorials > Flash MX 2004 > ActionScript 2.0 | ||||||||
|
||||||||
|
|
ActionScript 2.0 |
|
||||||
07. Dynamic vs. Static classesThe dynamic class modifier allows a class to be extended (i.e. allows new properties and methods to be added). For JScript.NET programmers, this is the same as expando. For classes that are not dynamic (such as Math), the user cannot add new methods or properties to it. In ActionScript 1.0, it is quite common to see code that is added to the prototype object of a class, such as: MovieClip.prototype.someNewMethod = function() {
// do something to the movieclip
};
In ActionScript 2.0, this type of extension is not allowed. You can, however,
create a subclass and add new methods and properties to it.
Here is an example of extending Math to create a new method called max() that finds the maximum value of any number of parameters (instead of being limited to two as in the original Math.max(), see Math2.as): dynamic class Math2 extends Math {
// store a reference to Math.max()
private static var oMax:Function = Math.max;
Unlike the original Math class, I've chosen to make this class dynamic, so that it can be extended. Use this method like using other Math methods, except now it belongs to the Math2 class: trace(Math2.max(70,6,3,12,82,9,28,5));
|
||||||||
©2003 Ultrashock.com - All rights reserved |