每个 Javascript 函数都必须返回一个值吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17337064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:56:15  来源:igfitidea点击:

Does every Javascript function have to return a value?

javascriptreturn-valuenetbeans-7

提问by trejder

I'm using Netbeans to add professional-like comments to each function, I write. So I begin each of it with /**and then I press Enterto let Netbeans fulfill default comment scheme for following function.

我正在使用 Netbeans 为每个函数添加专业的注释,我写道。所以我开始每一个,/**然后我按下Enter让 Netbeans 实现以下功能的默认评论方案。

Up until now I've been using this only for PHP language and in this case Netbeans was always adding @returns {type}part in comment scheme only, if following PHP function really included returnstatement. On so called "procedures" (functions that does not return any value) this part was missing.

到目前为止,我一直只将它用于 PHP 语言,在这种情况下,Netbeans 总是@returns {type}仅在注释方案中添加部分,如果遵循 PHP 函数确实包含return语句。在所谓的“过程”(不返回任何值的函数)中,这部分丢失了。

Today I tried the same thing for Javascript function and Netbeans added @returns {undefined}part to comment scheme even though following function does not return anything.

今天我对 Javascript 函数尝试了同样的事情,@returns {undefined}即使以下函数没有返回任何内容,Netbeans 也将部分添加到注释方案中。

This confused me. Does Netbeans suggests this way, that every Javascript function has to return anything? What should I do? Ignore (or delete) that comment scheme part or follow suggestion (if this is suggestion at all) and add return false;in the end of such function, though it is useless for me?

这让我很困惑。Netbeans 是否建议这种方式,即每个 Javascript 函数都必须返回任何内容?我该怎么办?忽略(或删除)该评论方案部分或遵循建议(如果这是建议的话)并添加return false;到此类功能的末尾,尽管它对我没有用?

回答by Elias Van Ootegem

The short answer is no.

最简洁的答案是不。

The realanswer is yes: the JS engine has to be notified that some function has finished its business, which is done by the function returning something. This is also why, instead of "finished", a function is said to "have returned".
A function that lacks an explicit return statement will return undefined, like a C(++) function that has no return value is said (and its signature reflects this) to return void:

真正的答案是肯定的:JS引擎必须要通知一些功能已经完成了它的业务,这是由函数返回有所作为。这也是为什么函数被称为“已返回”而不是“完成”的原因。 缺少显式 return 语句的函数将 return ,就像没有返回值的 C(++) 函数一样(其签名反映了这一点)返回:
undefinedvoid

void noReturn()//return type void
{
    printf("%d\n", 123);
    return;//return nothing, can be left out, too
}

//in JS:
function noReturn()
{
    console.log('123');//or evil document.write
    return undefined;//<-- write it or not, the result is the same
    return;//<-- same as return undefined
}

Also, in JS, like in most every language, you're free to simply ignore the return value of a function, which is done an awful lot:

此外,在 JS 中,就像在大多数语言中一样,您可以随意忽略函数的返回值,这做了很多:

(function()
{
    console.log('this function in an IIFE will return undefined, but we don\'t care');
}());
//this expression evaluates to:
(undefined);//but we don't care

At some verylow level, the return is translated into some sort of jump. If a function really returned nothingat all, there would be no way of knowing what and when to call the next function, or to call event handlers and the like.

在一些非常低的水平上,返回被转化为某种跳跃。如果一个函数真的什么都不返回,就没有办法知道什么时候调用下一个函数,或者调用事件处理程序等。

So to recap: No, a JS function needn't return anything as far as your code goes. But as far as the JS engines are concerned: a function alwaysreturns something, be it explicitly via a returnstatement, or implicitly. If a function returns implicitly, its return value will always be undefined.

所以总结一下:不,就您的代码而言,JS 函数不需要返回任何内容。但就 JS 引擎而言:函数总是返回一些东西,无论是通过return语句显式还是隐式。如果函数隐式返回,则其返回值将始终是未定义的。

回答by rhapsodyn

No, returnis not necessary.

不,不需要退货

But no returnactually return the undefined

但是没有返回实际上返回了undefined

回答by T.J. Crowder

Does every Javascript function have to return a value?

每个 Javascript 函数都必须返回一个值吗?

No, they don't. It's true that deep in the specification, these are all slightlydifferent:

不,他们没有。的确,在规范的深处,这些都略有不同:

function foo() {
}
function foo() {
    return;
}
function foo() {
    return undefined;
}

...but the result of callingeach of them is the same: undefined. So in pragmatic terms:

...但结果他们每个人都是一样的:undefined。所以从实用的角度来说:

  1. You don't have to write a return, you can just let code execution "fall off the end" of the function
  2. If you're returning undefined, specifically, you an just write return;
  3. When calling a function, you cannot tell (in code) whether execution fell off the end, ended with return;, or ended with return undefined;; they all look exactly the same to your calling code
  1. 你不必写一个return,你可以让代码执行“从函数的末尾”
  2. 如果你要回来undefined,特别是,你只要写return;
  3. 调用函数时,您无法(在代码中)判断执行是从结尾处结束、以 结束return;还是以return undefined;;结束。它们看起来与您的调用代码完全相同

Re the spec: Specifically, when a function's execution falls off the end, in the spec that's a "normal" completion; but return;and return value;are both "return" completions with an associated value (undefined), which is (ever so slightly) different. But the difference is eliminated by the semantics of calling a function, which say:

重新规范:具体来说,当一个函数的执行结束时,在规范中这是一个“正常”的完成;但是return;return value;都是具有关联值 ( undefined) 的“返回”完成,这是(稍微)不同的。但是调用函数的语义消除了差异,它说:

...

  1. If result.[[Type]] is return, return NormalCompletion(result.[[Value]]).
  2. ReturnIfAbrupt(result).
  3. Return NormalCompletion(undefined).

...

  1. 如果result.[[Type]] 是return,则返回 NormalCompletion( result.[[Value]])。
  2. ReturnIfAbrupt(结果)。
  3. 返回 NormalCompletion( undefined)。

So there's no difference you can observe in code.

因此,您可以在代码中观察到没有区别。

回答by mohkhan

No, you don't have to return something for every function. It is optional and upto how you write your code logic.

不,您不必为每个函数都返回一些东西。它是可选的,取决于您如何编写代码逻辑。