在函数调用中展开 ...args 数组

发布于 2024-10-07 17:45:43 字数 720 浏览 0 评论 0原文

我正在对 JavaScript 方法进行大量的ExternalInterface 调用,并有一个辅助函数来执行此操作:

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

但这意味着 JavaScript 方法只会传递一个参数 - 参数数组 - 这意味着我必须更改 JavaScript 来适应这一点,例如of:

function example(argument1, argument2)
{

}

我最终得到:

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

我想做的是展开传递给 JSCall 方法的参数数组,以便每个参数单独传递给ExternalInterface 调用,这样:

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

工作原理如下:

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

I'm making numerous ExternalInterface calls to JavaScript methods and have a helper function for doing so:

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

However this means the JavaScript method will only be passed one argument - the array of arguments - meaning I have to change the JavaScript to accomodate this, e.g. instead of:

function example(argument1, argument2)
{

}

I end up with:

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

What I'd love to do is unroll the arguments array being passed to the JSCall method so that each argument is passed individually to the ExternalInterface call, such that:

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

works like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人练习一个人 2024-10-14 17:45:43

要从 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'});

...which会在 firebug/webkit 控制台上打印类似 Testing 123 true Object 的内容。

这已经过测试并且肯定有效,因为我正在实际项目中使用它。

To call a javascript function from flash with multiple arguments, all you have to do is:

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

If you're taking the arguments from a variable list of arguments of another function, then you can use:

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);
}

Somewhere else you would call:

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

...which would print something like Testing 123 true Object on your firebug/webkit console.

This is tested and works for sure, as I'm using it in a real project.

青瓷清茶倾城歌 2024-10-14 17:45:43

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

ExternalInterface.call.apply( methodName, args );

这太疯狂了,它可能会起作用!

Hey Cameron, have you tried using Function.apply()? Try this:

ExternalInterface.call.apply( methodName, args );

It's so crazy, it just might work!

岛徒 2024-10-14 17:45:43

在 JavaScript 中,此 Function.call.apply(foo, [that, test, bla]) 的工作方式类似于 foo.call(that, test, bla) 但因为 ExternalInterface.call 不等于 Function.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 中对此进行测试。

In JavaScript this Function.call.apply(foo, [that, test, bla]) works like foo.call(that, test, bla) but since ExternalInterface.call is not equal to Function.prototype.call we need to use a different approach here.

// 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

Note: I have not tested this in ActionScript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文