0

In ES6, it's possible to use ...arguments to pass all arguments of one function to another, e.g.

function foo(a, b) {
  return bar(...arguments);
}

function bar(a, b) {
  return a + b;
}

foo(1, 2); // returns 3

I "discovered" this idiom myself, and am wondering if there are any downsides to it, or whether it's considered a bad practice?

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • *wondering if there are any downsides to it* I don't think so, its example is even given in doc https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator#Syntax – gurvinder372 Feb 07 '18 at 12:25
  • 1
    Questions such as `Is this bad practice` are not on-topic questions as they lead to `primarily opinion-based` answers . See What Questions [**Are On-Topic**](https://stackoverflow.com/help/on-topic) and which ones [**Are not On-Topic**](https://stackoverflow.com/help/dont-ask) for more details. - In general I would check the [**arguments documentation**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) to see if it is something you can use and if it is compatible with browsers you need to run it on as well as if there is any `downsides` to using it. – Nope Feb 07 '18 at 12:25
  • @Dónal Then you should know better at 114k points then to ask off topic questions on SO – Nope Feb 07 '18 at 12:30
  • 1
    @Nope: A question like this *can* be on-topic (IMHO), but Dónal, not if you're asking for *opinions*. But if not too open-ended, asking for known technical, objective downsides to a technique can be okay. It's that "if not too open-ended" that's tricky... :-) – T.J. Crowder Feb 07 '18 at 12:32
  • IMHO: All of it is answered in the documentation found with a simple search and anything not answered in the documentation should lead to be able to ask a precise question focusing on a specific issue. IMHO: This question sounds/reads like little research was done beforehand, such as reading the documentation. Just my personal opinion :/ – Nope Feb 07 '18 at 12:35

3 Answers3

7

...am wondering if there are any downsides to it...

Not really, other than a couple of points to be aware of:

  • arguments in loose mode has some minor performance problems (nothing to worry about in normal situations).
  • Arrow functions don't have their own arguments, so that won't work in an arrow function; you'll either get a ReferencError (if the arrow function is at the top level) or the arguments from the nearest containing function function (or method) (because arrows close over arguments [and this and super]), which may not be what you want.

You're using spread notation, so you might consider using rest notation as well:

function foo(...args) {
  return bar(...args);
}

function bar(a, b) {
  return a + b;
}

console.log(foo(1, 2)); // returns 3

That doesn't have the (minor) performance issues with arguments, works in arrow functions, and is clear about what you mean.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks for the answer, what's "loose mode"? – Dónal Feb 07 '18 at 12:32
  • 1
    @Dónal: Loose mode is the default mode of JavaScript. To adopt *strict* mode, you put `"use strict";` at the top of the compilation unit or function you want it to apply to. More in [this question's answers](https://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it) and [on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode). – T.J. Crowder Feb 07 '18 at 12:34
  • Thanks, I was aware of strict mode, but didn't realise the default mode is known by this name – Dónal Feb 07 '18 at 12:36
  • 1
    @Dónal: :-) You'll also hear "sloppy" mode, but "loose" is slightly more common I think (probably because loose/strict are antonyms; for sloppy to make sense, it would have needed to be `"use neat"` or `"use tidy"` **;-)** ). I think those are the only two terms for it I've heard. – T.J. Crowder Feb 07 '18 at 12:39
1

arguments variable in the function contains all arguments passed to the function. This can be used if you have various number of parameters and you don't know how many.

Starting from ES6 if you want to pass into the function various number of parameters, you can use rest parameters.

function foo(...rest) {
  return bar(...rest);
}

function bar(a, b) {
  return a + b;
}

foo(1, 2);
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
1

not really any downsides, you just have to make sure that if your arguments is an array, that it is in the right order for the arguments of the function that you are calling.

Anis Jonischkeit
  • 914
  • 7
  • 15