我什么时候应该在es6箭头函数中使用`return`?

时间:2015-03-05 22:56:42

标签: javascript function return ecmascript-6 arrow-functions

新的es6 arrow functionsreturn在某些情况下是隐含的:

  

表达式也是该函数的隐式返回值。

在什么情况下我需要将return与es6箭头功能一起使用?

6 个答案:

答案 0 :(得分:226)

杰克逊在一个类似的问题上有部分answered this

  

隐式返回,但仅限于没有阻止。

     
      
  • 当单行扩展到多行并且程序员忘记添加return时,这将导致错误。
  •   
  • 隐含的回归在语法上是模棱两可的。 (name) => {id: name}返回对象{id: name} ...对吗?错误。它返回undefined。那些括号是一个明确的块。 id:是一个标签。
  •   

我会在此添加block的定义:

  

块语句(或其他语言的复合语句)用于对零个或多个语句进行分组。该块由一对花括号分隔。

<强>实施例

// 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') 

答案 1 :(得分:14)

我理解这个经验法则......

  

对于有效转换的函数(参数的单行操作),return是隐式的。

候选人是:

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

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

对于其他操作(需要块的多个单行,返回必须是显式的

答案 2 :(得分:7)

这里还有另一种情况。

在React中编写功能组件时,可以使用括号包装隐式返回的JSX。

const FunctionalComponent = () => (
  <div>
    <OtherComponent />
  </div>
);

答案 3 :(得分:2)

箭头函数允许您隐式返回:返回值而不必使用return关键字。

当函数体中有一个在线语句时,它可以工作:

&#13;
&#13;
const myFunction = () => 'test'

console.log(myFunction()) //'test'
&#13;
&#13;
&#13;

另一个例子,返回一个对象(记得在括号中包裹花括号以避免它被认为是包装函数体括号):

&#13;
&#13;
const myFunction = () => ({value: 'test'})

console.log(myFunction()) //{value: 'test'}
&#13;
&#13;
&#13;

答案 4 :(得分:1)

这是另一个给我带来麻烦的情况。

// the "tricky" way
const wrap = (foo) => (bar) => {
  if (foo === 'foo') return foo + ' ' + bar;
  return 'nofoo ' + bar;
}

这里我们定义了一个返回匿名函数的函数。“棘手”位是外部函数的函数主体(以(bar)=>开头的部分)在视觉上看起来像一个“块”,但是不是。既然不是,隐式返回就开始了。

以下是自动换行的方式:

// use wrap() to create a function withfoo()
const withfoo = wrap('foo');
// returns: foo bar
console.log(withfoo('bar'));

// use wrap() to create a function withoutfoo()
const withoutfoo = wrap('bar');
// returns: nofoo bar
console.log(withoutfoo('bar'));

我将其解压缩以确保自己理解的方法是“取消范围划分”功能。

这是第一个代码块的语义等效项,只是使wrap()的主体显式返回即可。该定义产生与上述相同的结果。这是点连接的地方。将上面的第一个代码块与下面的代码块进行比较,很明显,箭头函数本身被视为an expression, not a block, and has the implied return

// the explicit return way
const wrap = (foo) => {
  return (bar) => {
    if (foo === 'foo') return foo + ' ' + bar;
    return 'nofoo ' + bar;
  }
}

wrap的完全未经收窄的版本将是这样的,虽然它不如粗箭头所示的版本那么紧凑,但似乎更容易理解。

// the "no arrow functions" way
const wrap = function(foo) {
  return function(bar) {
    if (foo === 'foo') return foo + ' ' + bar;
    return 'nofoo ' + bar;
  };
};

最后,对于其他可能不得不阅读我的代码以及将来的我来说,我想我更喜欢使用非箭头版本,该版本乍一看就可以直观地理解,而不是那些需要有很多想法(在我的案例中是实验)。

答案 5 :(得分:0)

省略括号 {} 并从箭头函数返回关键字是可以的,如果: (1) 在 return 语句之前你不会有任何代码(例如赋值语句),并且 (2) 您将返回单个实体 [注意:单个实体可以是多行。如果是这样,那么您只需要常规括号(),如下例所示:

posts.map(post => (
  <li key={post.id}>
    {post.title}
  </li>
))
相关问题