Page 128 - JavaScript
P. 128

When using implicit returns, object literals must be wrapped in parenthesis so that the curly braces
        are not mistaken for the opening of the function's body.


         const foo = () => { bar: 1 } // foo() returns undefined
         const foo = () => ({ bar: 1 }) // foo() returns {bar: 1}


        Explicit Return


        Arrow functions can behave very similar to classic functions in that you may explicitly return a
        value from them using the return keyword; simply wrap your function's body in curly braces, and
        return a value:


         const foo = x => {
           return x + 1;
         }

         foo(1); // -> 2


        Arrow functions as a constructor



        Arrow functions will throw a TypeError when used with the new keyword.


         const foo = function () {
           return 'foo';
         }

         const a = new foo();

         const bar = () => {
           return 'bar';
         }

         const b = new bar(); // -> Uncaught TypeError: bar is not a constructor...


        Read Arrow Functions online: https://riptutorial.com/javascript/topic/5007/arrow-functions
































        https://riptutorial.com/                                                                               85
   123   124   125   126   127   128   129   130   131   132   133