9

I need to pass arguments onto a callback function as real arguments. How do I do that? callback requires a variable number of arguments.

Example function like this:

var func = function (callback) {
  doSomething(function () {
    // the goal is to pass on the arguments object to the callback function as real arguments
    callback(arguments)
  });
}

Thanks in advance.

It might be a similar question to this: Is it possible to send a variable number of arguments to a JavaScript function?

But I didn't understand that question nor the answers.

Edit: If possible, I would like to not pollute global.

Community
  • 1
  • 1
Harry
  • 52,711
  • 71
  • 177
  • 261

1 Answers1

19

Use apply to invoke the callback so the array items gets passed as individual arguments to the function.

callback.apply(this, arguments);

apply takes the context, and an array as an argument, and each item of the array can be passed as a named argument of the function being invoked.

function two(first, second) {
    alert(first), alert(second);
}

two.apply(null, ["Hello", "World"]); // alerts "Hello", then "World"

Implementations of ES3 required that the second argument to apply be either an array, or an arguments object. ES5 makes it more liberal in that as long as it resembles an array - has a length property, and corresponding integer indexes, it will work.

Anurag
  • 140,337
  • 36
  • 221
  • 257
  • Why apply null instead of this? – Harry Mar 28 '11 at 09:21
  • allpy takes two arguments, the obejct which becomes this in the called function and an array whic becmes the arguments. So your line should read callback.apply(this, Array.prototype.slice.apply(arguments)); – HBP Mar 28 '11 at 09:22
  • Yep, I've messed up with this before seeing your answer came up with something similar: http://jsfiddle.net/yahavbr/HYTJL/ – Shadow The GPT Wizard Mar 28 '11 at 09:23
  • I'm not using `this` in any special manner inside the function `two`, so it doesn't really matter what we pass as context to `apply`. We could've passed in anything - `two.apply("dontCare", ["Hello", "World"])`. – Anurag Mar 28 '11 at 09:25
  • @Hans - thanks for the correction, I'm missing the context as the first argument. – Anurag Mar 28 '11 at 09:26
  • The question : http://stackoverflow.com/questions/5456709/create-shortcut-to-console-log confirmsthat slicing and dicing is not necessary : just need callback.apply (this, arguments); – HBP Mar 28 '11 at 09:29
  • @Hans @Harry - If you are passing an array or the arguments object as the second parameter to apply, then you should be fine as per the specs in ECMAScript 3rd ed [(sec 15.3.4.3)](http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf). For ECMAScript 5th ed [(sec 15.3.4.3)](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf), anything that looks like an array - has a length property, and integer indexes corresponding to the length, should work. – Anurag Mar 28 '11 at 09:41