[英]Is there a way to pass the member function of another class as a parameter to a function in as3?
For example, I have a function in class A:例如,我在 class A 中有一个 function:
private function functionA(f:Function):void
{
var objB:B;
objB.f();
}
Is there a way to pass a non-static public member function of class B as a parameter to functionA?有没有办法将 class B 的非静态公共成员 function 作为参数传递给函数 A? (from inside class A, of course) I know such syntax exists in c++, but not sure if you can do this in flex/as3 (当然,来自 class A 内部)我知道 c++ 中存在这样的语法,但不确定您是否可以在 flex/as3 中执行此操作
Sure:当然:
var a : A = new A();
var b : B = new B();
a.functionA(b.functionB);
...
private function functionA(f:Function):void
{
f();
// or
f(1, "hi");
}
The instance associated with the function is carried with it.与 function 关联的实例随身携带。 If you need to call the function on a different instance call f.apply(instance, [1, "hi"])
如果您需要在不同的实例上调用 function,请调用f.apply(instance, [1, "hi"])
AS3 has no concept of delegates or function-signatures-as-a-type so you'll need to know the arguments to pass in. AS3 没有委托或函数签名作为类型的概念,因此您需要知道 arguments 才能传入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.