Javascript-将变量参数传递给函数

Javascript-将变量参数传递给函数,javascript,parameters,parameter-passing,optional-parameters,Javascript,Parameters,Parameter Passing,Optional Parameters,我希望能够创建两个函数,BaseFunction和CallbackFunction,其中BaseFunction包含一组可变参数: BaseFunction(arg1, arg2, ....) { //Call the Callback function here } 回调函数接收回相同的参数: CallbackFunction(value, arg1, arg2, ...) { } 如何将参数从基函数传递到回调函数?这种方法的作用是: BaseFunction(arg1, arg

我希望能够创建两个函数,BaseFunction和CallbackFunction,其中BaseFunction包含一组可变参数:

BaseFunction(arg1, arg2, ....)
{
    //Call the Callback function here
}
回调函数接收回相同的参数:

CallbackFunction(value, arg1, arg2, ...)
{

}
如何将参数从基函数传递到回调函数?

这种方法的作用是:

BaseFunction(arg1, arg2, ....)
{
  CallbackFunction(value, arguments);
}
但是,
CallbackFunction
需要接受数组:

CallbackFunction(value, argsArray)
{
  //...
}
用于使用参数数组调用函数

BaseFunction(arg1, arg2, ....)
{
    // converts arguments to real array
    var args = Array.prototype.slice.call(arguments);
    var value = 2;  // the "value" param of callback
    args.unshift(value); // add value to the array with the others
    CallbackFunction.apply(null, args); // call the function
}
演示:


有关
参数
值的更多信息,请查看。

以传递任意数量的参数:

function BaseFunction() {
     CallbackFunction.apply( {}, Array.prototype.slice.call( arguments ) );
}

是的,这是我现在的退路。如果可能的话,我更喜欢用另一种方式。实际上,您可以只将
参数
传递给
应用
,而不调用
切片
。@这不一定适用于所有浏览器。参数是类似于数组的对象,但不是数组。例如,版本4之前的firefox有一个bug,现在已经修复,它不允许您传递arguments对象。没错,不过我希望人们有最新版本的浏览器。表示可以传递“类似数组”的对象。编辑:没关系:
“注意:大多数浏览器,包括Chrome 14和Internet Explorer 9,仍然不接受类似数组的对象,并且会引发异常。”