ECMAScript 6 arrow function that returns an object

后端 未结 6 2153
花落未央
花落未央 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 05:44

    You can always check this out for more custom solutions:

    x => ({}[x.name] = x);
    
    0 讨论(0)
  • 2020-11-21 05:49

    the right ways

    1. normal return object
    
    const getUser = user => {return { name: user.name, age: user.age };};
    
    const user = { name: "xgqfrms", age: 21 };
    
    console.log(getUser(user));
    //  {name: "xgqfrms", age: 21}
    
    
    1. (js expressions )
    
    const getUser = user => ({ name: user.name, age: user.age });
    
    const user = { name: "xgqfrms", age: 21 };
    
    console.log(getUser(user));
    //  {name: "xgqfrms", age: 21}
    
    

    explain

    image

    The same answer can be found here!

    https://github.com/lydiahallie/javascript-questions/issues/220

    https://mariusschulz.com/blog/returning-object-literals-from-arrow-functions-in-javascript

    0 讨论(0)
  • 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

    0 讨论(0)
  • 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!
    }
    
    0 讨论(0)
  • 2020-11-21 06:00

    Issue:

    When you do are doing:

    p => {foo: "bar"}
    

    JavaScript interpreter thinks you are opening a multi-statement code block, and in that block, you have to explicitly mention a return statement.

    Solution:

    If your arrow function expression has a single statement, then you can use the following syntax:

    p => ({foo: "bar", attr2: "some value", "attr3": "syntax choices"})
    

    But if you want to have multiple statements then you can use the following syntax:

    p => {return {foo: "bar", attr2: "some value", "attr3": "syntax choices"}}
    

    In above example, first set of curly braces opens a multi-statement code block, and the second set of curly braces is for dynamic objects. In multi-statement code block of arrow function, you have to explicitly use return statements

    For more details, check Mozilla Docs for JS Arrow Function Expressions

    0 讨论(0)
  • 2020-11-21 06:06

    You must wrap the returning object literal into parentheses. Otherwise curly braces will be considered to denote the function’s body. The following works:

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

    You don't need to wrap any other expression into parentheses:

    p => 10;
    p => 'foo';
    p => true;
    p => [1,2,3];
    p => null;
    p => /^foo$/;
    

    and so on.

    Reference: MDN - Returning object literals

    0 讨论(0)
提交回复
热议问题