The use of brackets in javascript arrow function declaration

给你一囗甜甜゛ 提交于 2019-12-13 10:23:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!