在Javascript中的可变函数内调用可变函数?
我有两个函数a()和b(),它们都是可变函数,比方说当我像这样调用函数a()时:在Javascript中的可变函数内调用可变函数?,javascript,function,variadic,Javascript,Function,Variadic,我有两个函数a()和b(),它们都是可变函数,比方说当我像这样调用函数a()时: a(arg0, arg1, arg2, arg3, ...., argn); 然后函数b()也将在a()内调用,但在a()的参数列表中没有第一个参数“arg0”: 有什么办法吗?每个JavaScript函数实际上只是另一个“对象”(JavaScript意义上的对象),并附带了一个apply方法(请参阅)。你可以这样做 b = function(some, parameter, list) { ... } a =
a(arg0, arg1, arg2, arg3, ...., argn);
然后函数b()也将在a()内调用,但在a()的参数列表中没有第一个参数“arg0”:
有什么办法吗?每个JavaScript
函数实际上只是另一个“对象”(JavaScript意义上的对象),并附带了一个apply
方法(请参阅)。你可以这样做
b = function(some, parameter, list) { ... }
a = function(some, longer, parameter, list)
{
// ... Do some work...
// Convert the arguments object into an array, throwing away the first element
var args = Array.prototype.slice.call(arguments, 1);
// Call b with the remaining arguments and current "this"
b.apply(this, args);
}
相关:酷,我刚刚做了一个快速测试,效果很好。谢谢你的帮助!
b = function(some, parameter, list) { ... }
a = function(some, longer, parameter, list)
{
// ... Do some work...
// Convert the arguments object into an array, throwing away the first element
var args = Array.prototype.slice.call(arguments, 1);
// Call b with the remaining arguments and current "this"
b.apply(this, args);
}