我有两个函数a()和b(),两者都是可变函数,比如当我调用函数a()时这样:
a(arg0, arg1, arg2, arg3, ...., argn);
然后函数b()也将在a()中被调用,但是在()的参数列表中没有第一个参数“arg0”:
b(arg1, arg2, arg3, ...., argn);
有什么办法吗?
答案 0 :(得分:20)
每个JavaScript function
实际上只是另一个“对象”(JavaScript意义上的对象),并附带apply
方法(请参阅Mozilla's documentation)。你可以这样做......
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);
}