mrK*_*mrK 2 javascript parameters parameter-passing optional-parameters
我希望能够创建两个函数,BaseFunction和CallbackFunction,其中BaseFunction接受一组变量参数:
BaseFunction(arg1, arg2, ....)
{
//Call the Callback function here
}
Run Code Online (Sandbox Code Playgroud)
和回调函数返回相同的参数:
CallbackFunction(value, arg1, arg2, ...)
{
}
Run Code Online (Sandbox Code Playgroud)
如何将参数从基函数传递给回调函数?
使用apply调用函数的参数数组.
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
}
Run Code Online (Sandbox Code Playgroud)
有关该arguments值的更多信息,请查看mozilla的文档.