简体   繁体   中英

The use of brackets in javascript arrow function declaration

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)?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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