8

I have two function a() and b(), both are variadic functions, let say when I call function a() like this :

a(arg0, arg1, arg2, arg3, ...., argn);

then the function b() will be called as well inside a(), but without the first argument "arg0" in the arguments list of a() :

b(arg1, arg2, arg3, ...., argn);

Is there any way for it?

Teiv
  • 2,345
  • 10
  • 32
  • 43
  • 1
    Related: http://stackoverflow.com/questions/960866/converting-the-arguments-object-to-an-array-in-javascript – Ray Toal Jul 24 '11 at 18:17

1 Answers1

23

Every JavaScript function is really just another "object" (object in the JavaScript sense), and comes with an apply method (see Mozilla's documentation). You can thus do something like this....

b = function(some, parameter, list) { ... }

a = function(some, longer, parameter, list)
{
   // ... Do some work...

   // Convert the arguments object into an array, throwing away the first element
   var args = Array.prototype.slice.call(arguments, 1);

   // Call b with the remaining arguments and current "this"
   b.apply(this, args);
}
Adam Wright
  • 47,483
  • 11
  • 126
  • 149