obviously arguments.length does not work.
I can change the signature to f: (...args) => { if (args.length>0) { ..}; };
But this removes the parameter information from the function declaration .
Any better way ?
length property provides the number of arguments actually passed to a function.
When you call a function in JavaScript, you can pass in any number of arguments, regardless of what the function declaration specifies. There is no function parameter limit. In the above function, if we pass any number of arguments, the result is always the same because it will take the first two parameters only.
Arrow functions do not have an arguments binding. However, they have access to the arguments object of the closest non-arrow parent function. Named and rest parameters are heavily relied upon to capture the arguments passed to arrow functions.
Arguments binding Arrow functions do not have an arguments binding.
The short answer is: "no", or "maybe".
The longer answer is: from MDN :
An arrow function expression has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own
this
,arguments
,super
, ornew.target
). Arrow functions are always anonymous. These function expressions are best suited for non-method functions and they can not be used as constructors.
The main use for arrow functions are for callbacks, to execute code as if it was executed in its parent context. Thus preventing the annoying and ugly const that = this;
requirement and does it implicitly.
For this reason, and since it is executed anonymously, within the parent's context, there is no arguments
, or rather the value is of the parent's context. Arrow functions solve only a general use case, not every one.
// const that = this;
var that = this;
...
f: function (a, b, c) {
...
}
...
arguments
, etc.)that
(or some other var name) instead of this
.that
reference...
f: (function (a, b, c) {
...
}).bind(this)
...
this
is availablearguments
, etc.)...
f: (...args) => {
const [a, b, c] = args;
...
}
g: (a, b, c, ...others) => {
...
}
...
arguments
instanceYou've already got this figured out, spread operator + .length
will give you the information you need if you are using arrow functions. I would consider the particular use-case you are presenting (where you need to preserve parameter information and know the length of the arguments) as more well suited to a non-arrow function:
f: function(a, b, c) { console.log(arguments.length); }
If you are using arrow functions to preserve the parent context, you can always add .bind(this)
to the end (and incur a small performance penalty).
I don't know exactly what you need to accomplish, but if you aren't trying to write a function with unknown arity, perhaps default arguments would allow you to handle cases where a user forgets a required parameter? ...or you could pack your arguments in an object and use Object.keys
to determine the length:
let f = (args) => console.log(Object.keys(args).length);
f({ a: 1, b: 2, c: 3 });
This would preserve parameter information and allow you to ascertain the length of the arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With