3

I've got a lot of function that act as an intermediate entry-point for other functions (i.e functions that just call other functions).

  • How can I call the inner function with the same arguments as the parent function without respecifying them?

    var foo = function(arg1, arg2) {
      fooBar(argumentsOfFoo);  
    }
    
  • A possible solution is passing arrays but I'd like to avoid that.

nicholaswmin
  • 21,686
  • 15
  • 91
  • 167

1 Answers1

4

You use .apply() and the arguments object:

var foo = function(arg1, arg2) {
  fooBar.apply(this, arguments);  
}

In modern versions of Javascript, you can use rest parameters:

function foo(...args) {
  fooBar(...args);  
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979