trajectory
trajectory

Reputation: 1509

extract parameters from callback outside of the callback

How can I extract the arguments (a, b) of a callback-function and add another parameter (c) to the function-call?

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) } );

Upvotes: 2

Views: 408

Answers (1)

Jordão
Jordão

Reputation: 56467

The only way I can think of is to pass the parameters to perform itself and then let it pass them along to the callback:

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);

Upvotes: 2

Related Questions