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

2018-12-31 14:49发布

The new es6 arrow functions say return is implicit under some circumstances:

The expression is also the implicit return value of that function.

In what cases do I need to use return with es6 arrow functions?

4条回答
与君花间醉酒
2楼-- · 2018-12-31 15:23

Jackson has partially answered this in a similar question:

Implicit return, but only if there is no block.

  • This will result in errors when a one-liner expands to multiple lines and the programmer forgets to add a return.
  • Implicit return is syntactically ambiguous. (name) => {id: name}returns the object {id: name}... right? Wrong. It returns undefined. Those braces are an explicit block. id: is a label.

I would add to this the definition of a block:

A block statement (or compound statement in other languages) is used to group zero or more statements. The block is delimited by a pair of curly brackets.

Examples:

// returns: undefined
// explanation: an empty block with an implicit return
((name) => {})() 

// returns: 'Hi Jess'
// explanation: no block means implicit return
((name) => 'Hi ' + name)('Jess')

// returns: undefined
// explanation: explicit return required inside block, but is missing.
((name) => {'Hi ' + name})('Jess')

// returns: 'Hi Jess'
// explanation: explicit return in block exists
((name) => {return 'Hi ' + name})('Jess') 

// returns: undefined
// explanation: a block containing a single label. No explicit return.
// more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label
((name) => {id: name})('Jess') 

// returns: {id: 'Jess'}
// explanation: implicit return of expression ( ) which evaluates to an object
((name) => ({id: name}))('Jess') 

// returns: {id: 'Jess'}
// explanation: explicit return inside block returns object
((name) => {return {id: name}})('Jess') 
查看更多
高级女魔头
3楼-- · 2018-12-31 15:25

Arrow functions allow you to have an implicit return: values are returned without having to use the return keyword.

It works when there is a on-line statement in the function body:

const myFunction = () => 'test'

console.log(myFunction()) //'test'

Another example, returning an object (remember to wrap the curly brackets in parentheses to avoid it being considered the wrapping function body brackets):

const myFunction = () => ({value: 'test'})

console.log(myFunction()) //{value: 'test'}

查看更多
明月照影归
4楼-- · 2018-12-31 15:33

I understand this rule-of-thumb ...

For functions that are effectively transforms (one-line-manipulations of arguments), return is implicit.

Candidates are:

// square-root 
value => Math.sqrt(value)

// sum
(a,b) => a+b

For other operations (more than one-liners that require a block, return has to be explicit

查看更多
情到深处是孤独
5楼-- · 2018-12-31 15:34

There's another case here.

When writing a functional component in React, you can use parentheses to wrap implicitly returned JSX.

const FunctionalComponent = () => (
  <div>
    <OtherComponent />
  </div>
);
查看更多
登录 后发表回答