gpt4 book ai didi

javascript - 在 JS 中显式指定 return 语句与根本没有 return 语句有什么区别?

转载 作者:行者123 更新时间:2023-11-29 14:50:10 26 4
gpt4 key购买 nike

我正在维护一些旧代码,我注意到在很多情况下,老程序员将 return; 语句作为他大部分函数的最后一行。这有什么好处吗?我觉得这是浪费空间,所以我一直在删除它们。其中之一通常更快吗?

最佳答案

来自ECMAScript Language Specification :

When the [[Call]] internal method for a Function object F is called with a this value and a list of arguments, the following steps are taken:

  1. Let funcCtx be the result of establishing a new execution context for function code using the value of F's [[FormalParameters]] internal property, the passed arguments List args, and the this value as described in 10.4.3.
  2. Let result be the result of evaluating the FunctionBody that is the value of F's [[Code]] internal property. If F does not have a [[Code]] internal property or if its value is an empty FunctionBody, then result is (normal, undefined, empty).
  3. Exit the execution context funcCtx, restoring the previous execution context.
  4. If result.type is throw then throw result.value.
  5. If result.type is return then return result.value.
  6. Otherwise result.type must be normal. Return undefined.

换句话说,如果被调用的函数没有明确的return语句,那么它会隐式返回undefined

关于javascript - 在 JS 中显式指定 return 语句与根本没有 return 语句有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27046684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com