Curly Brackets in Arrow Functions

家住魔仙堡 提交于 2019-11-26 03:34:47

问题


can someone, please explain the following:

I\'m following Dan Abramov\'s lectures & doing the exercises.

The code works fine, however, the tests fail when the following particular function is written with curly brackets **{ }**.

    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. Please refer to line 31 onwards.


回答1:


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




回答2:


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.

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.



来源:https://stackoverflow.com/questions/35440265/curly-brackets-in-arrow-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!