问题
In ES6/ES2015 arrow functions can be declared with or without brackets around the parameter.
ie:
var foo_1 = myVar => {
return myVar + 1;
}
or:
var foo_2 = (myVar) => {
return myVar + 2;
}
What I would like to know is: what is the difference (if any)?
回答1:
There is no differences in your example.
You need parentheses if you
- have no parameter:
() => ...
- have multiple parameters:
(foo, bar) => ...
- use destructuring:
({foo}) => ...
- use default values:
(foo = 42) => ...
- have a rest parameter:
(...bar) => ...
- have any combination of the above
In other words, whenever you do not have a single identifier-only parameter.
来源:https://stackoverflow.com/questions/37848328/the-use-of-brackets-in-javascript-arrow-function-declaration