ECMAScript 6 arrow functions

TA3
var getTempItem = id => ({ id: id, name: "Temp" });

I know the above arrow function is equivalent to:

var getTempItem = function(id) {

    return {
        id: id,
        name: "Temp"
   };
};

But I'm a bit confused about the following

const Todo = ({ onClick, completed, text }) => (
  <li
    onClick={onClick}
    style={{
      textDecoration: completed ? 'line-through' : 'none'
    }}
  >
    {text}
 </li>
)

Why are the function arguments wrapped in curly braces, and the the function body is wrapped only in parentheses?

Platinum Azure

A few syntactic sugar elements of ES6 are at play here:

  • Parameter destructuring: The function actually takes one object, but before the function is executed, its sole object parameter is deconstructed into three variables. Basically, if the argument passed to the function is called obj, then the onClick variable is assigned the value of obj.onClick, and same with the other named destructure variables.
  • Concise arrow bodies: An arrow function that only needs one expression can use the concise form. For example, x => 2*x is an arrow function that returns its input times two. However, the ES6 grammar specification says that a curly brace after the arrow must be interpreted as a statement block. So in order to return an object using a concise body, you have to wrap the object expression in parentheses.
  • JSX: Parentheses are commonly used around JSX expressions that need to span more than one line.

Bonus: One manner in which arrow functions are different from function declarations and function expressions is in the fact that in an arrow function (even one with a non-concise body), the values of arguments and this are the same as the containing scope. So calling an arrow function with .call or .apply will have no effect, and you need to use rest parameters if you want your arrow function to take a variable number of arguments.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

ECMAScript 6 arrow function that returns an object

From Dev

What does Arrow function do: ECMAScript 6

From Dev

ES6 arrow functions

From Dev

Converting ECMAScript 6's arrow function to a regular function

From Dev

es6 arrow functions debugger statement

From Dev

Are ES6 arrow functions incompatible with Angular?

From Dev

ECMA Script 6 Arrow functions as object properties

From Dev

ES6 arrow functions not working on the prototype?

From Dev

javascript es6 double arrow functions

From Dev

What is the best way to sum arrays using ECMASCRIPT 6 Generator/Functions

From Dev

Argument types in functions versus in arrow functions in ES6

From Dev

es6katas.org Kata #6: arrow functions - binding

From Dev

Arrow Functions and This

From Java

Methods in ES6 objects: using arrow functions

From Java

Is it possible to export Arrow functions in ES6/7?

From Java

Using _ (underscore) variable with arrow functions in ES6/Typescript

From Dev

Immediate function using JavaScript ES6 arrow functions

From Java

When should I use `return` in es6 Arrow Functions?

From Dev

How to run ES6 code with arrow functions in Safari?

From Dev

What does "this" refer to in arrow functions in ES6?

From Dev

How to use ES6 arrow functions with promise binding (bluebird)

From Dev

Official information on `arguments` in ES6 Arrow functions?

From Dev

Explain effect of ES6 class constructor and arrow functions

From Dev

jQuery .each() function with ES6 arrow functions

From Dev

Jump to ES6 arrow functions in Sublime text editor

From Dev

Can't use fat arrow functions (ES6) in react

From Dev

This not working as expected in nested arrow functions in ES6

From Dev

Precedence of yield in EcmaScript 6

From Dev

ECMAScript 6: what is WeakSet for?

Related Related

  1. 1

    ECMAScript 6 arrow function that returns an object

  2. 2

    What does Arrow function do: ECMAScript 6

  3. 3

    ES6 arrow functions

  4. 4

    Converting ECMAScript 6's arrow function to a regular function

  5. 5

    es6 arrow functions debugger statement

  6. 6

    Are ES6 arrow functions incompatible with Angular?

  7. 7

    ECMA Script 6 Arrow functions as object properties

  8. 8

    ES6 arrow functions not working on the prototype?

  9. 9

    javascript es6 double arrow functions

  10. 10

    What is the best way to sum arrays using ECMASCRIPT 6 Generator/Functions

  11. 11

    Argument types in functions versus in arrow functions in ES6

  12. 12

    es6katas.org Kata #6: arrow functions - binding

  13. 13

    Arrow Functions and This

  14. 14

    Methods in ES6 objects: using arrow functions

  15. 15

    Is it possible to export Arrow functions in ES6/7?

  16. 16

    Using _ (underscore) variable with arrow functions in ES6/Typescript

  17. 17

    Immediate function using JavaScript ES6 arrow functions

  18. 18

    When should I use `return` in es6 Arrow Functions?

  19. 19

    How to run ES6 code with arrow functions in Safari?

  20. 20

    What does "this" refer to in arrow functions in ES6?

  21. 21

    How to use ES6 arrow functions with promise binding (bluebird)

  22. 22

    Official information on `arguments` in ES6 Arrow functions?

  23. 23

    Explain effect of ES6 class constructor and arrow functions

  24. 24

    jQuery .each() function with ES6 arrow functions

  25. 25

    Jump to ES6 arrow functions in Sublime text editor

  26. 26

    Can't use fat arrow functions (ES6) in react

  27. 27

    This not working as expected in nested arrow functions in ES6

  28. 28

    Precedence of yield in EcmaScript 6

  29. 29

    ECMAScript 6: what is WeakSet for?

HotTag

Archive