Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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]);.

like image 914
Derek Avatar asked May 16 '26 00:05

Derek


1 Answers

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()

like image 54
canon Avatar answered May 17 '26 12:05

canon