胖箭头函数(=>)的语法,在身体周围使用或不使用{} [英] Syntax of fat arrow functions (=>), to use or not to use {} around the body

查看:122
本文介绍了胖箭头函数(=>)的语法,在身体周围使用或不使用{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看此代码- https://facebook.github. io/react-native/docs/network.html

return fetch('https://facebook.github.io/react-native/movies.json')
      .then((response) => response.json())
      .then((responseJson) => {
        return responseJson.movies;
      })

据我了解,.then((response) => response.json())转换为:

.then(function(response) {
    return response.json()
}

但是我不知道这意味着什么?其中有一个额外的{}

but I can't figure out what does this translate into? there is an extra {} in it

.then((responseJson) => {
        return responseJson.movies;
      })

推荐答案

如果不使用大括号括住箭头函数的主体,它将对表达式求值并隐式返回结果.如果将其用大括号括起来,则结果不会隐式返回,而必须显式执行.

If you don't wrap the body of an arrow function with curly brackets, it will evaluate the expression and return the result implicitly. If you wrap it with curly brackets, the result is not implicitly returned and you have to do it explicitly.

因此,第二部分等于"以下内容:

For this reason, the second part 'equals' the following:

.then(function(responseJson) {
    return responseJson.movies;
})

这篇关于胖箭头函数(=>)的语法,在身体周围使用或不使用{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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