Syntax of fat arrow functions (=>), to use or not to use {} around the body

后端 未结 5 1053
梦谈多话
梦谈多话 2020-12-07 04:40

I am looking at this code - https://facebook.github.io/react-native/docs/network.html

return fetch(\'https://facebook.github.io/react-native/movies.json\')
          


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 05:22

    As a sidenote, if you want your function to return an object literal, you'll have to go with the extra curly braces and the explicit return:

    foo => { bar: "baz" } // will not work!
    

    This example will not work as the curly braces will not be interpreted as the delimiting characters of an object literal, but as the delimters of a block. Inside a block, bar: "baz" obviously is a syntax error. So to return { bar: "baz" } you need the xtra curly braces and the explicit return:

    foo => { return { bar: "baz" } } // will work
    

提交回复
热议问题