When should you use return statement in ES6 arrow functions?
P粉771233336
P粉771233336 2023-08-22 13:20:02
0
2
648
<p>The new ES6 arrow functions say that in some cases, <code>return</code> is implicit: </p> <blockquote> <p>This expression is also the implicit return value of the function. </p> </blockquote> <p>In what situations do I need to use <code>return</code> in an ES6 arrow function? </p>
P粉771233336
P粉771233336

reply all(2)
P粉258083432

I understand this rule of thumb...

The candidates are:

// 平方根
value => Math.sqrt(value)

// 求和
(a,b) => a+b

For other operations (if multiple lines of code are required, an explicit return value is required)

P粉012875927

Jackson partially answered this question in a similar question:

I would like to add the definition of block:

Example:

// 返回:undefined
// 解释:一个空的带有隐式返回的块
((name) => {})() 

// 返回:'Hi Jess'
// 解释:没有块意味着隐式返回
((name) => 'Hi ' + name)('Jess')

// 返回:undefined
// 解释:块内需要显式返回,但是缺少了
((name) => {'Hi ' + name})('Jess')

// 返回:'Hi Jess'
// 解释:块内有显式返回
((name) => {return 'Hi ' + name})('Jess') 

// 返回:undefined
// 解释:一个包含单个标签的块。没有显式返回。
// 更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label
((name) => {id: name})('Jess') 

// 返回:{id: 'Jess'}
// 解释:隐式返回表达式 ( ),其求值为一个对象
((name) => ({id: name}))('Jess') 

// 返回:{id: 'Jess'}
// 解释:块内有显式返回对象
((name) => {return {id: name}})('Jess')
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template