Wrapping a Vararg Method in ActionScript

雨燕双飞 提交于 2019-12-11 15:54:23

问题


I have a vararg method that I'd like to act as a proxy for another vararg method, but I'm not sure how to do it. Here's the basic code:

class MyClass {

   public function a(...args:*):* {
      // other code
      b(args);
      // other code
   }

   public function b(...args:*):* {
      // do stuff with args
   }

}

I'm porting the code from Java, where the type system knows that the arguments are actually supposed to be Strings, not arrays, so it can figure out to invoke b() by directly passing the arguments, and everything works how you'd expect.

But in ActionScript, the argument array gets wrapped into another array when invoked through the proxy method.

So, when b() is called directly, the array is only one level deep. But when b() is invoked through a(), the array is two levels deep.

Does anyone know of a trick for getting around this?

(NOTE: in my real code, a() and b() are actually in separate classes, and I really don't want to touch the implementation of b() at all. I can rewrite a() to my heart's content, but b() shouldn't change.)


回答1:


Well I can't claim to be any good at ActionScript (haven't used it in years..)

But if nothing else you can do something like..

class Test {
    function a(...args:*):* { b.apply(this, args); }
    function b(...args:*):* { trace(args[1]); }
}

In previous versions you could use 'arguments' to pass all passed arguments through the apply method, but that seems to have been removed in AS3.



来源:https://stackoverflow.com/questions/509638/wrapping-a-vararg-method-in-actionscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!