pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Errors/Bad_return

image/svg+xml" href="https://developer.mozilla.org/favicon.svg" />

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

SyntaxError: return not in function

return 语句在函数外部被调用时,JavaScript 会抛出“return not in function”异常。

错误信息

SyntaxError: Illegal return statement (V8-based)
SyntaxError: return not in function (Firefox)
SyntaxError: Return statements are only valid inside functions. (Safari)

错误类型

SyntaxError

什么地方出错了?

return 语句在函数外部被调用了。可能是某处缺失了花括号?return 语句必须位于函数内部,因为它会终止函数执行并指定一个值返回给函数调用者。

示例

缺少大括号

js
function cheer(score) {
  if (score === 147)
    return "最大!";
  }
  if (score > 100) {
    return "世纪!";
  }
}

// SyntaxError: return not in function

花括号乍一看之下似乎是正确的,但这段代码在第一个 if 语句后缺少了一个 {。正确的写法应该是:

js
function cheer(score) {
  if (score === 147) {
    return "最大!";
  }
  if (score > 100) {
    return "世纪!";
  }
}

参见

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy