栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaScript何时应在es6箭头函数中使用“ return”?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaScript何时应在es6箭头函数中使用“ return”?

隐式返回,但仅当没有块时才返回。
* 当单线扩展到多行并且程序员忘记添加时,这将导致错误

return

* 隐式返回在语法上是模棱两可的。
(name) => {id: name}
返回对象
{id:name}
…对吗?错误。它返回
undefined
。这些括号是一个明确的块。
id:
是一个标签。



我会在此添加一个块的定义:

块语句(或其他语言的复合语句)用于将零个或多个语句分组。该块由一对大括号分隔。

例子

// returns: undefined// explanation: an empty block with an implicit return((name) => {})()// returns: 'Hi Jess'// explanation: no block means implicit return((name) => 'Hi ' + name)('Jess')// returns: undefined// explanation: explicit return required inside block, but is missing.((name) => {'Hi ' + name})('Jess')// returns: 'Hi Jess'// explanation: explicit return in block exists((name) => {return 'Hi ' + name})('Jess')// returns: undefined// explanation: a block containing a single label. No explicit return.// more: https://developer.mozilla.org/en-US/docs/Web/Javascript/Reference/Statements/label((name) => {id: name})('Jess')// returns: {id: 'Jess'}// explanation: implicit return of expression ( ) which evaluates to an object((name) => ({id: name}))('Jess')// returns: {id: 'Jess'}// explanation: explicit return inside block returns object((name) => {return {id: name}})('Jess')


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/411267.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号