1

I have a method defined, say abc, which accepts a function name, say xyz and the arguments to that function as its arguments. Now, the abc method needs to call xyz with the arguments received.

xyz can be any method and could be having any number of arguments, also, xyz cannot be using arguments keyword. rather, the arguments for the method should be passed as separate variables.

Ex: xyz will be defined as:

var xyz = function(a,b){//code for processing a and b}

I am calling the abc method with the first argument as the function to be invoked and rest of the arguments as arguments to be passed to the function while calling it. In abc, I am making use of arguments keyword to access the dynamic number of arguments sent. But from there I am not sure how to call the next function with the arguments.

Jojodmo
  • 23,357
  • 13
  • 65
  • 107

1 Answers1

1

Use the apply method of functions to call it with a variable argument list:

function abc(funcName) {
    funcName.apply(null, [].slice.call(arguments, 1));
}

You could also pass along your current context by supplying the first argument:

function abc(funcName) {
    funcName.apply(this, [].slice.call(arguments, 1));
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • my funcName will be present in the arguments variable. Since array operations are not supported in javascript how can i remove the first argument and pass the rest of the arguments to funcName function? – user3295429 Feb 11 '14 at 03:37
  • I fixed the answer to use `arguments.slice(1)`. – Barmar Feb 11 '14 at 03:38