JavaScript返回值

Every function returns a value, which by default is undefined.

每个函数都返回一个值,默认情况下为undefined

Undefined return value

Any function is terminated when its lines of code end, or when the execution flow finds a return keyword.

当任何函数的代码行结束或执行流找到return关键字时,该函数将终止。

When JavaScript encounters this keyword it exits the function execution and gives control back to its caller.

当JavaScript遇到此关键字时,它将退出函数执行并将控制权交还给其调用者。

If you pass a value, that value is returned as the result of the function:

如果传递一个值,则该值作为函数的结果返回:

const dosomething = () => {
  return 'test'
}
const result = dosomething() // result === 'test'

You can only return one value.

您只能返回一个值。

To simulate returning multiple values, you can return an object literal, or an array, and use a destructuring assignment when calling the function.

为了模拟返回多个值,您可以返回对象常量数组 ,并在调用函数时使用解构分配。

Using arrays:

使用数组:

Destructuring using arrays

Using objects:

使用对象:

Destructuring using objects

翻译自: https://flaviocopes.com/javascript-return-values/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值