Passing arguments forward to another javascript function

Viewed 225491

I've tried the following with no success:

function a(args){
    b(arguments);
}

function b(args){
    // arguments are lost?
}

a(1,2,3);

In function a, I can use the arguments keyword to access an array of arguments, in function b these are lost. Is there a way of passing arguments to another javascript function like I try to do?

5 Answers

If you want to only pass certain arguments, you can do so like this:

Foo.bar(TheClass, 'theMethod', 'arg1', 'arg2')

Foo.js

bar (obj, method, ...args) {
  obj[method](...args)
}

obj and method are used by the bar() method, while the rest of args are passed to the actual call.

This one works like a charm.

function a(){
    b(...arguments);
}

function b(){
    for(var i=0;i<arguments.length;i++){
       //you can use arguments[i] here.
    }
}

a(1,2,3);
Related