2007年6月17日星期日

Proxy Class

ActionScript 3 introduces a new class to take the place of ActionScript 1 and ActionScript 2's addProperty method and __resolve handler for objects. This class is the Proxy Class (flash.utils.Proxy).

In ActionScript 1 and ActionScript 2:

  • addProperty() - used to dynamically add a getter/setter property for an object or class. This dynamically created a variable that was set and whose value was retrieved through functions passed into the addProperty call.
  • __resolve - a handler that, when defined for an object, would be called when a property or method was accessed that did not exist for that object. This let you capture unresolved references from an object and react accordingly


In ActionScript 3 you create a class that extends Proxy to take advantage of all its features. They include

  • Capturing property setting
  • Capturing property getting
  • Capturing property checking (if exists)
  • Capturing property deletion
  • Capturing method calls
  • Capturing decendant access
  • Capturing attribute checking
  • Handling property iteration


Though these features hold a lot of advange over what was available with AS1 and AS2, there are some drawbacks to using Proxy:

  • Instance typing is limited since classes need to extend Proxy; you tend to have to rely on interfaces
  • You cannot have Proxy classes that inherit from classes that don't already inherit from Proxy
  • Display objects cannot be Proxy classes


Proxy classes are generally used for variable container classes that need extra the flexibility.

The Proxy class will be covered in more detail in later tips

0 评论: