简体   繁体   English

JS ES6中()=>和_ =>以及(_)=>之间的差异

[英]Difference between ()=> and _=> and (_)=> in JS ES6

I have notice that when I want to write a fat arrow function " => " I can do _=> , ()=> or (_)=> and my code functions the same (at least for my use cases) 我注意到当我想写一个胖箭头函数“ => ”时,我可以做_=>()=>(_)=>并且我的代码功能相同(至少对于我的用例)

Is there an actual difference between them? 它们之间是否存在实际差异? If yes, which one should I use? 如果是, 我应该使用哪一个? I have been using ()=> most of the time, but then one day I saw someone's code using _=> and I thought it looked cool, so I started using it too. 我一直在使用()=> ,但有一天我看到某人的代码使用了_=>而且我觉得它看起来很酷,所以我也开始使用它了。

I saw this medium article https://medium.freecodecamp.org/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26 where the author states you can use _=> or ()=> but doesn't specify if there's a difference. 我看到这篇中篇文章https://medium.freecodecamp.org/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26作者声明你可以使用_=>()=>但未指定是否存在差异。

The general form of a fat-arrow function is fat-arrow函数的一般形式是

(parameter-list) => function-body

If you don't have any parameters, you use a pair of empty parentheses: 如果您没有任何参数,则使用一对空括号:

() => {}

If you have a single parameter it's: 如果你有一个参数,它是:

(x) => {}

Since _ is a valid identifier in JavaScript, you can do: 由于_是JavaScript中的有效标识符,因此您可以执行以下操作:

(_) => {}

Now, a special rule applies: If you have only one parameter, you can skip the parentheses, so you get: 现在,适用一条特殊规则:如果您只有一个参数,则可以跳过括号,这样您就得到:

_ => {}

Please note that this is only valid if you have a single parameter, ie for two you always have to specify the parentheses: 请注意,这仅在您有一个参数时才有效,即对于两个参数,您始终必须指定括号:

(x, y) => {}

Now, on the right side, if your entire function only consists of a single statement with a return , such as 现在,在右侧,如果您的整个函数只包含一个带有return语句,例如

x => { return x; }

you can omit the curly braces and the return : 你可以省略花括号和return

x => x

At least, this is true if on the right side you don't try to return an object, which would look like this (this code won't work!): 至少,如果在右侧你没有尝试返回一个对象,这将是这样的(这段代码不起作用!):

x => { value: x }

The reason why this does not work is that JavaScript can not distinguish this from a function body, which also uses curly braces, so now you have to wrap it in parentheses: 这不起作用的原因是JavaScript无法将它与函数体区分开来,函数体也使用花括号,所以现在你必须将它包装在括号中:

x => ({ value: x })

I think that's pretty much everything you need to know about the syntax of fat arrow functions. 我认为这几乎是你需要知道的关于胖箭头函数语法的一切。

You should not use something just because it looks cool. 你不应该只是因为它看起来很酷而使用的东西。

_ is often used to tell a linter (or reader) that you do not use an certain argument on purpose. _通常用于告诉linter(或读者)你没有故意使用某个参数。

So if you have a callback with two arguments and you only use the second one you would write (_, arg) => console.log(arg) because if you would write (foo, arg) => console.log(arg) then the linter would complain about foo not being used. 因此,如果你有一个带有两个参数的回调而你只使用第二个参数,你会写(_, arg) => console.log(arg)因为如果你要编写(foo, arg) => console.log(arg)然后linter会抱怨没有使用foo

And some APIs might have different behavior depending on the number of arguments the callback has (the middlewares of expressjs are an example for that), so if the API checks the length property of the callback then you might need to use a placeholder like _ . 并且一些API可能具有不同的行为,具体取决于回调所具有的参数数量(expressjs的中间件就是一个示例),因此如果API检查回调的length属性,那么您可能需要使用像_这样的占位符。

 var cb1 = _ => {}; var cb2 = () => {}; console.log(cb1.length) console.log(cb2.length) 

Arrow functions take any number of parameters, just like "regular" functions. 箭头函数可以使用任意数量的参数,就像“常规”函数一样。 When there are no parameters, this is indicated by () => {} , similarly to function() {} . 当没有参数时,这由() => {} ,类似于function() {} If you have exactly one parameter, though, the parenthesis may be omitted. 但是,如果只有一个参数,则可省略括号。 This leads to a syntax like _ => {} like you've noted — this is identical to (_) => {} , as an underscore is a valid identifier (just like foo ). 这会导致类似_ => {}的语法,就像你已经注意到的那样 - 这与(_) => {} ,因为下划线是一个有效的标识符(就像foo一样)。

The runtime difference in your specific situation seems to be none, as you're (presumably) not passing any parameters, in which case _ is undefined. 您的特定情况的运行时差异似乎没有,因为您(可能)没有传递任何参数,在这种情况下_是未定义的。 You can easily verify this: 您可以轻松验证这一点:

 const foo = _ => console.log(_); foo(5); // logs 5 foo(); // logs undefined 

That's not to say you should do it, just that it doesn't make a difference for now. 这并不是说你应该这样做,只是因为它现在没有任何区别。 If you added a parameter later, it could wreak havoc. 如果您稍后添加了参数,则可能会造成严重破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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