Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function with same arguments as parent [duplicate]

Tags:

javascript

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.

like image 997
nicholaswmin Avatar asked Aug 25 '26 14:08

nicholaswmin


1 Answers

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);  
}
like image 170
jfriend00 Avatar answered Aug 28 '26 05:08

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!