JavaScript ES6中的箭头函数-带或不带大括号 [英] Arrrow functions in JavaScript ES6 - with and without curly braces

查看:142
本文介绍了JavaScript ES6中的箭头函数-带或不带大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像问有关ES6在函数语法上的区别-带花括号和不带花括号.

Like to ask about the differences in ES6 in function syntax - with and without curly braces.

两个功能均正常工作

  1. 带有花括号的功能:

  1. function with a curly braces:

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

  • 没有花括号的相同功能:

  • same function without curly braces:

    const function = () => some code;
    

  • 谢谢.

    推荐答案

    欢迎使用Stackoverflow!

    welcome to Stackoverflow!

    实际上,这些不带花括号的功能是一些细微差别的简写形式.

    Indeed these functions without curly braces are a shorthand version that differs in some nuances.

    最重要的区别是:

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

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

    Sticking to the example above, the more classic version to write this would be () => {return 20 * 5}

    更多详细信息可以在此处找到示例.

    这篇关于JavaScript ES6中的箭头函数-带或不带大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆