This question already has answers here:
Single-Line to Multi-Line ES6 Fat Arrow Function?
                                
                                    (2个答案)
                                
                        
                        
                            Curly Brackets in Arrow Functions
                                
                                    (2个答案)
                                
                        
                        
                            How is () => {…} different from () => [duplicate]
                                
                                    (4个答案)
                                
                        
                2年前关闭。
            
        

想问一下ES6在函数语法上的区别-带花括号和不带花括号。

这两个功能都起作用:


带有花括号的功能:

const function = () => {some code;};

没有花括号的相同功能:

const function = () => some code;



谢谢。

最佳答案

欢迎来到Stackoverflow!

的确,这些没有花括号的功能是一些细微差别的简写形式。

最重要的区别是:


他们只能有一个陈述。 (例如() => 20 * 5
他们自动返回该语句的值(上面的示例将返回值100)


坚持上面的例子,写这个的更经典的版本是() => {return 20 * 5}

更多详细信息can be found here for example

09-20 19:19