actionscript-3 - How can I pass an arbitrary group of parameters “through” a method?
问题描述
I am using AIR v32.0. I have a class which extends netConnection. I need to override an existing method which receives an arbitrary group of parameters. I need to do a few odds & ends then let the original code handle the rest. I understand how to receive the parameters “…parameters” but I can’t find the proper way of calling the original code when I am done.
What I have so far which does not work is;
public override function call(strCommand:String, rspResponder:Responder,...parameters):void
{
trace("parameters.length=" + parameters.length)
if (parameters.length == 0)
{
super.call(strCommand, rspResponder);
}
else
{
super.call(strCommand, rspResponder, parameters);
}
}
The line;
super.call(strCommand, rspResponder);
Works as expected.
The line;
super.call(strCommand, rspResponder, parameters);
does not appear to be handled by the “call” method properly.
How can I properly pass the “…parameters” on to the “super.call” method of the netConnection class?
EDIT:
There is no error per se. What happens is it sends a message off to the FluorineFX web service in a format the web service is not expecting which then Ralphs on its shoes. The error is "Processing level error description Could not find a suitable method with name LoginCheck" If I remove the overidden method everything works fine.
When I look at the output results in Fiddler, there is a difference in some of the non printable characters between running it with & without the overridden method. But I cannot determine what the practical difference is.
Additional EDIT:
I have a very poor partial work around (at least it works), and perhaps this will spark someone's imagination. I would be embarrassed to put this into production.
public override function call(strCommand:String, rspResponder:Responder,...parameters):void
{
trace("parameters.length=" + parameters.length)
switch (parameters.length)
{
case 0:
super.call(strCommand, rspResponder);
break;
case 1:
super.call(strCommand, rspResponder, parameters[0]);
break;
case 2:
super.call(strCommand, rspResponder, parameters[0], parameters[1]);
break;
case 3:
super.call(strCommand, rspResponder, parameters[0], parameters[1], parameters[2]);
break;
case 4:
super.call(strCommand, rspResponder, parameters[0], parameters[1], parameters[2], parameters[3]);
break;
case 5:
super.call(strCommand, rspResponder, parameters[0], parameters[1], parameters[2], parameters[3], parameters[4]);
break;
case 6:
super.call(strCommand, rspResponder, parameters[0], parameters[1], parameters[2], parameters[3], parameters[4],
parameters[5]);
break;
case 7:
super.call(strCommand, rspResponder, parameters[0], parameters[1], parameters[2], parameters[3], parameters[4],
parameters[5], parameters[6]);
break;
case 8:
super.call(strCommand, rspResponder, parameters[0], parameters[1], parameters[2], parameters[3], parameters[4],
parameters[5], parameters[6], parameters[7]);
break;
case 9:
super.call(strCommand, rspResponder, parameters[0], parameters[1], parameters[2], parameters[3], parameters[4],
parameters[5], parameters[6], parameters[7], parameters[8]);
break;
case 10:
super.call(strCommand, rspResponder, parameters[0], parameters[1], parameters[2], parameters[3], parameters[4],
parameters[5], parameters[6], parameters[7], parameters[8], parameters[9]);
break;
}
}
解决方案
要传递以Array形式出现的额外...arguments ,您需要以特殊的Function.apply(...)方式调用该方法。问题是,AS3 中的函数(或者可能在一般的ECMA标准中)也是一个对象,因此它有一些自己的属性和方法,在某些情况下,它们允许执行一些看起来很奇怪的技巧。
public override function call(strCommand:String, rspResponder:Responder, ...parameters:Array):void
{
trace("parameters.length=" + parameters.length);
// Create a single deflated Array of incoming arguments.
var args:Array = [strCommand, rspResponder].concat(parameters);
// A special way of invoking a function where you provide
// all its arguments in a form of a single Array.
super.call.apply(this, args);
}
请记住,我没有测试过。另外,我从未尝试过apply(...)覆盖,但我认为理论上应该没有问题?
推荐阅读
- apache-kafka - 将 Kafka 升级到 2.5.0 后,不推荐使用 ZkUtils
- c# - Serilog,文件接收器,更改输出模板
- angular - NullInjectorError:没有提供 ConfigService (HttpClient)
- javascript - React 不会渲染从异步函数返回的东西
- git - git不会将一个文件同步到我的macOS本地,因为还有另一个大写文件名,我该怎么办?
- javascript - 尝试在本机反应中更改日历上的标记日期会导致语法错误
- flutter - Flutter - 如何放置动态图块并根据父宽度换行到下一行
- javascript - 覆盖全局字体大小:62%
- django - 来自单个表的两个模型的查询集
- python - 是否可以修改 Adafruit DHT22 库以支持 Raspberry Pi 4 Model B (BCM2711)?