Passing all parameters of one function to another? (javascript)

Go To StackoverFlow.com

3

How do you pass all the parameters of one function into another

function a(){
    arguments.length;// this is 1
    // how do I pass any parameter function a receives into another function
    b(what to put here?); // say pass all parameters function a receives to function b
}
a('asdf'); // this is okay

So if function a receives X number of parameters, each parameter is then passed into function b in the same order. So if a("1", "2"); , b("1", "2");, if a("1", 2, [3]); , b("1", 2, [3]);.

2012-04-05 02:16
by Derek


5

Use Function.prototype.apply(thisArg[, argsArray]), like so:

b.apply(this, arguments);

Now, arguments is an array-like object that has some other properties (like callee) apart from its n-index properties. So, you probably ought to use Array.prototype.slice() to turn the object into a simple array of arguments (though either will work in modern environments).

b.apply(this, Array.prototype.slice.call(arguments));

See also: Function.prototype.call()

2012-04-05 02:19
by canon


2

why not pass them as a single object? advantages to using a single object as a parameter is that

  • order is not necessary. unlike arguments where you have to know if it's the first, second, third in the receiving collection, in the object, they're just there. call them when you need them.
  • you never worry about how many were passed. you can send one object containing a lot of properties - and you only pass one argument between the caller and callee

ofcourse, you have to test them out if they exist before using

function foo(data){
    data.param1; //is foo
    data.param2; //is bar
    data.param3; //is baz

    //send params to bar
    bar(data);
}

function bar(info){
    info.param1; //is foo
    info.param2; //is bar
    info.param3; //is baz
}

//call function foo and pass object with parameters
foo({
    param1 : 'foo',
    param2 : 'bar',
    param3 : 'baz'
})
2012-04-05 02:25
by Joseph
When same functionality is been internally provided by browser javascript then why to worry again. Also, one could use slice and splice function to make alterations: http://stackoverflow.com/questions/3914557/passing-arguments-forward-to-another-javascript-functio - iMatoria 2012-09-27 02:51
I think named parameters are a way better paradigm overall, but in JavaScript the built-in way is numbered, at least with ES5. So it's a bit more intuitive to simply transfer the parameters as-is rather than wrapping and unwrapping objects. Of course some situations may warrant the object technique - Beejor 2015-11-11 23:36
@Beejor and I believe that this was answered back in 2012, and a better answer was already chosen - Joseph 2015-11-12 00:30
@JosephtheDreamer Yep, I was just posting an answer to your answer's question of "why not...". Even when a question is years old, people still read it and may benefit from additional discussion or answers. For example even though your answer wasn't chosen, it still has great value for special cases. And if you didn't post it back in 2012, it would have added value to this page even if someone else posted it later. :- - Beejor 2015-11-19 00:18
Ads