Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在函数调用中展开…args数组_Javascript_Flash_Actionscript 3_Externalinterface - Fatal编程技术网

Javascript 在函数调用中展开…args数组

Javascript 在函数调用中展开…args数组,javascript,flash,actionscript-3,externalinterface,Javascript,Flash,Actionscript 3,Externalinterface,我对JavaScript方法进行了大量外部接口调用,并为此提供了一个帮助函数: protected function JSCall( methodName:String, ...args ):void { try { ExternalInterface.call( methodName, args ); } … etc … } 但是,这意味着JavaScript方法将只传递一个参数(参数数组),这意味着我必须更改JavaScript以适应此情况,例如,而不是: func

我对JavaScript方法进行了大量外部接口调用,并为此提供了一个帮助函数:

protected function JSCall( methodName:String, ...args ):void
{
  try
  {
    ExternalInterface.call( methodName, args );
  }
  … etc …
}
但是,这意味着JavaScript方法将只传递一个参数(参数数组),这意味着我必须更改JavaScript以适应此情况,例如,而不是:

function example(argument1, argument2)
{

}
我的结局是:

function example(args)
{
  var argument1 = args[0];
  var argument2 = args[1];
}
我想做的是展开传递给
JSCall
方法的arguments数组,以便将每个参数分别传递给
ExternalInterface
调用,这样:

JSCall('example', ['one', 'two'])
工作原理如下:

ExternalInterface.call('example', 'one', 'two')

嘿,Cameron,你试过使用Function.apply()吗?试试这个:

ExternalInterface.call.apply( methodName, args );

这太疯狂了,可能会有用

在JavaScript中,这个
函数.call.apply(foo[that,test,bla])
的工作原理类似于
foo.call(that,test,bla)
,但是由于
ExternalInterface.call
不等于
函数.prototype.call,我们需要在这里使用不同的方法

// Let's 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中测试过这一点。

要使用多个参数从flash调用javascript函数,只需执行以下操作:

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'});
…这将在firebug/webkit控制台上打印类似于测试123 true Object的内容


这是经过测试并确实有效的,因为我正在一个真实的项目中使用它。

这确实有效,它是一个巧妙的黑客,我自己已经使用过几次了,看起来很奇怪,90%的人在第一次看到它时都不理解它,但嘿,它太棒了!您好,Mims/Ivo,不幸的是,虽然这不会导致任何编译/运行时闪存错误,但在尝试执行EI调用时会引发JavaScript错误@Ivo-您有使用此技术的真实示例吗?通过查看apply()的API文档,我可以了解以下内容的工作方式:
ExternalInterface.call.apply(null,[methodname,args])
但这与直接使用ExternalInterface.call没有帮助;)我想知道这是否有效:'ExternalInterface.call(method.apply,args);'还有,我的意思不是不尊重,也许你应该问问自己,这样做是否为自己省去了麻烦。如果只是为了避免添加try-catch语句,那么可以将其内联。如果有很多代码,可以调用prepForJSCall()之类的函数,然后执行ExternalInterface.call()。