Deploying an args array in a function call

I make numerous ExternalInterface calls for JavaScript methods and have a helper function for this:

protected function JSCall( methodName:String, ...args ):void
{
  try
  {
    ExternalInterface.call( methodName, args );
  }
  … etc …
}

However, this means that the JavaScript method will be passed only to one argument - an array of arguments - this means that I have to change the JavaScript to accommodate this, for example. instead:

function example(argument1, argument2)
{

}

As a result, I:

function example(args)
{
  var argument1 = args[0];
  var argument2 = args[1];
}

I would like the argument array to be passed to the method JSCall, so that each argument is passed individually for the call ExternalInterface, so that:

JSCall('example', ['one', 'two'])

works like:

ExternalInterface.call('example', 'one', 'two')
+3
source share
3 answers

javascript flash , :

ExternalInterface.call.apply(null, [functionName, arg1, arg2, ..., argn]);

, :

function JSCall(methodName:String, ...args):void
{
    if (ExternalInterface.available){
        args.unshift(methodName);
        ExternalInterface.call.apply(null, args);
    }

    //btw, you can do the same with trace(), or any other function
    args.unshift('Calling javascript function');
    trace.apply(null, args);
}

:

JSCall('console.log', 'Testing', 123, true, {foo:'bar'});

..., - Testing 123 true Object firebug/webkit.

, .

+3

, , Function.apply()? :

ExternalInterface.call.apply( methodName, args );

, !

+2

JavaScript Function.call.apply(foo, [that, test, bla]) foo.call(that, test, bla), ExternalInterface.call Function.prototype.call, .

// Let fake ExternalInterface
var funcs = {
    example: function(arg1, arg2) {
        console.log(arg1, arg2);
    }
};

var ExternalInterface = {};
ExternalInterface.call = function() {
    var f = funcs[arguments[0]];
    f.call.apply(f, arguments);
}


// This does crazy stuff.
function JSCall(func, args) {
    args.unshift(func);
    ExternalInterface.call.apply(ExternalInterface, args);
}

// These do the same now
ExternalInterface.call('example', 'one', 'two'); // one two
JSCall('example', ['one', 'two']); // one two

. ActionScript.

0

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


All Articles