开发者

Something like PHP's __call() in Actionscript

Suppose (as an example) that I have a class which I want to log all method calls to.

In PHP this can be accomplished quickly and easily with __call(), or in Python with decorators.

What would be the easiest way to accomplish the same t开发者_高级运维hing in Actionscript 3?


Extend flash.utils.Proxy and use the flash.utils.flash_proxy namespace. There's methods similar to __get, __set and methods for delete methods as well. For example, the __call method is:

override flash_proxy function callProperty(name:*, ...rest):*;

so if have a class that extends Proxy, you do:

var test:MyObject = new MyObject();
test.myMethodThatIsntDefined("param");

then callProperty will be called and name will be set to "myMethodThatIsntDefined" and "param" will be in the ...rest array.

The link to the asdoc has a simple implementation that should get you going. I typically use the Proxy class for something like an API. For example, back in the day I had a Flickr API wrapper that translated the name of the function call to an API method name in the Flickr API. Something like:

flickr.galleriesGetPhotos();

and in the callProperty I'd split on the first word to get the API name "flickr.galleries.get_photos". The names were different back then I think.


You can try using the Proxy class.

 dynamic class MyProxy extends Proxy {
 flash_proxy override function callProperty(name:*, ...rest):* {
   try {
     // custom code here
   }
   catch (e:Error) {
     // respond to error here
   }

}

Refer:http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/Proxy.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜