如何提取回调函数的参数(a,b)并添加另一个参数 参数(c)到函数调用?
function mycallback(a, b, c) {
// do stuff with a, b, c
}
function perform(callback) {
// I need to extract a and b here and add c:
// ???
//callback.apply(???);
}
perform( function() { mycallback(1, 2) } );
答案 0 :(得分:2)
我能想到的唯一方法是将参数传递给perform
本身然后让它传递给回调:
function perform(callback) {
var args = Array.prototype.slice.apply(arguments).splice(1);
// use args... add c with args.push(c)...
callback.apply(this, args);
}
perform(mycallback, 1, 2);