[英]good way to validate the number of arguments to an arrow function?
obviously arguments.length does not work.显然 arguments.length 不起作用。
I can change the signature to f: (...args) => { if (args.length>0) { ..}; };
我可以将签名更改为f: (...args) => { if (args.length>0) { ..}; };
f: (...args) => { if (args.length>0) { ..}; };
But this removes the parameter information from the function declaration .但这会从函数声明中删除参数信息。
Any better way ?有什么更好的方法吗?
The short answer is: "no", or "maybe".简短的回答是:“不”或“也许”。
The longer answer is: from MDN :更长的答案是:来自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
).与函数表达式相比,箭头函数表达式的语法更短,并且在词法上绑定了 this 值(不绑定自己的this
、arguments
、super
或new.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;
从而防止烦人和丑陋的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.出于这个原因,并且由于它是匿名执行的,因此在父级的上下文中,没有arguments
,或者更确切地说,值是父级的上下文。 Arrow functions solve only a general use case, not every one.箭头函数仅解决一般用例,而不是每个用例。
// const that = this;
var that = this;
...
f: function (a, b, c) {
...
}
...
arguments
, etc.)拥有自己的上下文(即arguments
等)that
(or some other var name) instead of this
.烦人的that
(或其他一些 var 名称)而不是this
。that
reference函数不能是外部的,否则会丢失that
引用...
f: (function (a, b, c) {
...
}).bind(this)
...
this
is available “正确” this
是可用的arguments
, etc.)拥有自己的上下文(即arguments
等)...
f: (...args) => {
const [a, b, c] = args;
...
}
g: (a, b, c, ...others) => {
...
}
...
arguments
instance非实arguments
实例You've already got this figured out, spread operator + .length
will give you the information you need if you are using arrow functions.您已经弄清楚了这一点,如果您使用箭头函数,展开运算符 + .length
将为您提供所需的信息。 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 ).如果您使用箭头函数来保留父上下文,则始终可以将.bind(this)
添加到末尾(并且会导致小的性能损失)。
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: ...或者您可以将参数打包在一个对象中并使用Object.keys
来确定长度:
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.这将保留参数信息并允许您确定参数的长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.