ECMAScript 6 arrow function that returns an object

后端 未结 6 2257
花落未央
花落未央 2020-11-21 05:24

When returning an object from an arrow function, it seems that it is necessary to use an extra set of {} and a return keyword because of an ambigui

6条回答
  •  萌比男神i
    2020-11-21 05:59

    If the body of the arrow function is wrapped in curly braces, it is not implicitly returned. Wrap the object in parentheses. It would look something like this.

    p => ({ foo: 'bar' })
    

    By wrapping the body in parens, the function will return { foo: 'bar }.

    Hopefully, that solves your problem. If not, I recently wrote an article about Arrow functions which covers it in more detail. I hope you find it useful. Javascript Arrow Functions

提交回复
热议问题