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.

1
  • instead of explaining can you post the code you have? Commented Feb 11, 2014 at 3:12

1 Answer 1

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));
}
Sign up to request clarification or add additional context in comments.

2 Comments

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?
I fixed the answer to use arguments.slice(1).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.