3
我知道使用動態添加到動態代理類的函數很容易,但我也希望能夠攔截對類型化函數的調用,就像您可以使用Python中的裝飾器一樣。下面的例子。我喜歡某種方式熬過「的callProperty」爲'typedFunc就像它已經經歷「dynFunc」,任何方式來攔截AS3中的類型函數調用?
package
{
import flash.display.Sprite;
[SWF(width = '400', height = '400')]
public class Test extends Sprite
{
public function Test()
{
var t:TypeTest = new TypeTest();
t.dynFunc = function dynFunc(s:String, i:int):Boolean { return true; };
t.typedFunc("a", 1);
t.dynFunc("b", 2);
}
}
}
import flash.utils.Proxy;
import flash.utils.flash_proxy;
internal dynamic class TypeTest extends Proxy
{
private var customs:Object = new Object();
override flash_proxy function callProperty(name:*, ...parameters):* {
var retval:* = (this[name] as Function).apply(null, parameters);
trace("called", name, "with", parameters);
return retval;
}
public function typedFunc(s:String, i:int):Boolean {
return false;
}
override flash_proxy function getProperty(name:*):* { return customs[name]; }
override flash_proxy function setProperty(name:*, value:*):void { customs[name] = value; }
}