ECMAScript 6 arrow function that returns an object

后端 未结 6 2138
花落未央
花落未央 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条回答
  •  别跟我提以往
    2020-11-21 06:00

    You may wonder, why the syntax is valid (but not working as expected):

    var func = p => { foo: "bar" }
    

    It's because of JavaScript's label syntax:

    So if you transpile the above code to ES5, it should look like:

    var func = function (p) {
      foo:
      "bar"; //obviously no return here!
    }
    

提交回复
热议问题