AS3变长参数展开以调用另一个具有var length args的函数

gui*_* 桂林 5 actionscript-3

这该怎么做

function foo(x:*, ...args):* {
}

function bar(x:*, ...args):* {
   foo(x, args); // how to expand args ? the args is an array now
}
Run Code Online (Sandbox Code Playgroud)

如何扩展args?当我打电话时bar(1,2,3),我希望它打电话foo(1,2,3),但它打电话foo(1,[2,3])

Cre*_*ers 4

function bar(x:*, ...args):* {
   args.unshift( x ); //add x to the front of the args array
   foo.apply( <scope>, args );
}
Run Code Online (Sandbox Code Playgroud)

如果foo和应该bar在同一个类中声明,否则它应该是声明类的实例,如果是全局函数它应该是<scope>thisfoofoonull

foo.apply( this, args );
//-- or --
foo.apply( myFooInstance, args );
//-- or --
foo.apply( null, args );
Run Code Online (Sandbox Code Playgroud)