[英]Curly Brackets in Arrow Functions
can someone, please explain the following:有人可以,请解释以下内容:
I'm following Dan Abramov's lectures & doing the exercises.我正在听 Dan Abramov 的讲座并做练习。
The code works fine, however, the tests fail when the following particular function is written with curly brackets **{ }**
.代码工作正常,但是,当以下特定 function用大括号
**{ }**
编写时,测试失败。
case 'toggleTodo' :
return (
state.map( (one) => {
oneTodo( one, action )
})
);
The same code works fine without curly brackets.相同的代码在没有大括号的情况下也能正常工作。
case 'toggleTodo' :
return (
state.map( (one) =>
oneTodo( one, action )
)
);
Here is the JsBin .这是 JsBin 。 Please refer to line 31 onwards.
请参考第 31 行之后的内容。
The pair of braces forms a block , containing a list of statements.这对大括号形成一个块,其中包含一个语句列表。 You need to use a
return
statement explicitly to make the function return something.您需要显式使用
return
语句来使函数返回某些内容。
If you omit the braces, the arrow function has a concise body , which consists solely of a single expression whose result will implicitly become the return value of the function.如果省略大括号,箭头函数有一个简洁的主体,它只包含一个表达式,其结果将隐含地成为函数的返回值。
case 'toggleTodo' :
return (
state.map( (one) =>
oneTodo( one, action )
)
);
is equal to:等于:
case 'toggleTodo' :
return (
state.map( (one) => {
return oneTodo( one, action )
})
);
see the return statement见退货声明
It's a fair practice to use curly brackets when there are multiple statements inside an arrow function. Use curly braces to make multiple statements a single block and avoid getting errors inside an arrow function.当箭头 function 内有多个语句时,使用花括号是一种公平的做法。使用花括号将多个语句作为一个块,避免在箭头 function 内出现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.