简体   繁体   English

验证箭头函数的参数数量的好方法?

[英]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 , or new.target ).函数表达式相比,箭头函数表达式的语法更短,并且在词法上绑定了 this 值(不绑定自己的thisargumentssupernew.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.箭头函数仅解决一般用例,而不是每个用例。


Solution 1 : legacy or "naïve" implementation解决方案 1:遗留或“幼稚”的实现

// const that = this;
var that = this;

...
  f: function (a, b, c) {
    ...
  }
...
  • Pros优点
  • (currently) slightly faster than arrow functions (目前)比箭头函数稍快
  • possess their own context (ie arguments , etc.)拥有自己的上下文(即arguments等)
  • can safely be implemented in all browsers and environments可以在所有浏览器和环境中安全地实现
  • Cons缺点
  • annoying that (or some other var name) instead of this .烦人的that (或其他一些 var 名称)而不是this
  • function cannot be external, or lose that reference函数不能是外部的,否则会丢失that引用

Solution 2 : function binding (ES5)方案二:函数绑定(ES5)

...
  f: (function (a, b, c) {
    ...
  }).bind(this)
...
  • Pros优点
  • "correct" this is available “正确” this是可用的
  • can pass any external function reference (ie reuse code)可以传递任何外部函数引用(即重用代码)
  • possess their own context (ie arguments , etc.)拥有自己的上下文(即arguments等)
  • 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)不支持 IE8 和其他浏览器(我不得不提到这一点,即使我对 IE8 不在乎)

Solution 3 : Destructuring assignment (ES6)解决方案 3:解构赋值 (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缺点
  • may require destructuring可能需要解构
  • (currently) slightly slower than a standard function (目前)比标准功能稍慢
  • not real arguments instance非实arguments实例
  • requires a transpiler (ex: Babel) to run in the browser, which will probably transpile into a "legacy" implementation需要一个转译器(例如:Babel)在浏览器中运行,这可能会转译为“遗留”实现

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM