admin管理员组文章数量:1310385
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 ?
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 ?
Share Improve this question asked Sep 11, 2016 at 1:03 kofifuskofifus 19.3k22 gold badges117 silver badges183 bronze badges 6-
3
Do you have to use an arrow function?
: function() { }.bind(this)
, though less performant would have the same effect and would provide you thearguments
object you need – Rob M. Commented Sep 11, 2016 at 1:10 - yes I have to thx – kofifus Commented Sep 11, 2016 at 4:50
-
What exactly do you want to validate? You never should count arguments anyway, treat
f()
the same asf(undefined)
. – Bergi Commented Sep 12, 2016 at 13:28 - Bergi - what I wanted to write is an assert function that easily validate the number of arguments and will work also in functions defined with arrow notation – kofifus Commented Sep 12, 2016 at 21:58
- > But this removes the parameter information from the function declaration < Sounds like you use typescript? How about method overloading? – lordvlad Commented Sep 25, 2022 at 22:30
2 Answers
Reset to default 6The short answer is: "no", or "maybe".
The longer answer is: from MDN :
An arrow function expression has a shorter syntax pared 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.
Solution 1 : legacy or "naïve" implementation
// const that = this;
var that = this;
...
f: function (a, b, c) {
...
}
...
- Pros
- (currently) slightly faster than arrow functions
- possess their own context (i.e.
arguments
, etc.) - can safely be implemented in all browsers and environments
- Cons
- annoying
that
(or some other var name) instead ofthis
. - function cannot be external, or lose
that
reference
Solution 2 : function binding (ES5)
...
f: (function (a, b, c) {
...
}).bind(this)
...
- Pros
- "correct"
this
is available - can pass any external function reference (i.e. reuse code)
- possess their own context (i.e.
arguments
, etc.) - Cons
- (currently) slightly slower than arrow functions
- unexpected results if the function reference was previously bound
- no support for IE8 and perhaps other browsers (I had to mention this, even if I cannot care less for IE8)
Solution 3 : Destructuring assignment (ES6)
...
f: (...args) => {
const [a, b, c] = args;
...
}
g: (a, b, c, ...others) => {
...
}
...
- Pros
- anonymous function
- access to parent context seemlessly
- exhibit a similar behaviour
- Cons
- signature tells you nothing about how it "should" be called
- may require destructuring
- (currently) slightly slower than a standard function
- not real
arguments
instance - requires a transpiler (ex: Babel) to run in the browser, which will probably transpile into a "legacy" implementation
You'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 acplish, 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.
本文标签: javascriptgood way to validate the number of arguments to an arrow functionStack Overflow
版权声明:本文标题:javascript - good way to validate the number of arguments to an arrow function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741801581a2398256.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论