Go ... to the NetConnection call

I want to pass a rest in a netconnection call, something like this:

public function requestData(service : String, ...params) : void
{
 nc.call(service, params);
}

this does not work, since the call expects each parameter to be separated by commas, for example:

nc.call(service, params[0], params[1], params[2]);

I read some application messages, but I can not find a solution for this particular case.

+3
source share
1 answer

Try the following:

public function requestData(service : String, ...params) : void
{
    var applyArgs:Array = params && params.length > 0 
                            ? [service].concat(params) 
                            : [service];
    nc.call.apply(nc,applyArgs);    
}

I have not tested this particular piece of code, but since the second argument that Function :: apply uses is an array that will be converted to a parameter list, this should work (unless I made some kind of stupid mistake ... no help the compiler back in SO!).

, applyArgs service . , : - , .

+3

Source: https://habr.com/ru/post/1749072/


All Articles