What are the advantages/disadvantages for creating a top level function in ES6 with arrows or without?

后端 未结 2 1034
花落未央
花落未央 2020-12-03 17:21

What are the advantages/disadvantages to create a top level function in ES6/ES2015 in those different ways? Or is this just a matter of taste/style guide etc?

Option

2条回答
  •  甜味超标
    2020-12-03 17:50

    The two main differences between old functions and ES6 arrow-functions is the length of code (arrow functions reduce the boilerplate in your case and it is IMO more elegant as you can event omit the return keyword) and the way these functions work with the context of the function call.

    I would write the function as const square = n => n*n;. But generally, there is no such thing as 'preferred way to create a top level function in ES6/ES2015'.

提交回复
热议问题