每个Javascript函数都必须返回一个值吗? [英] Does every Javascript function have to return a value?

查看:135
本文介绍了每个Javascript函数都必须返回一个值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 Enter to let Netbeans fulfill default comment scheme for following function.

到目前为止,我一直只在PHP语言中使用它,在这种情况下,Netbeans总是添加 @returns {type} 仅参与评论方案,如果以下PHP函数确实包含 return 语句。在所谓的程序(没有返回任何值的函数)这部分缺失。

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 return statement. On so called "procedures" (functions that does not return any value) this part was missing.

今天我尝试了同样的Javascript函数,Netbeans添加了 @returns {undefined} 评论方案的一部分,即使以下函数没有返回任何内容。

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.

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

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?

推荐答案

答案很简短。

The short answer is no.

真正的答案是肯定的:必须通知JS引擎某些功能已经完成其业务,这是由函数返回的东西完成的。这也是为什么,而不是完成,一个函数被称为已经返回

缺乏明确回报的函数语句将返回 undefined ,就像没有返回值的C(++)函数(并且其签名反映了这一点)返回 void

The real answer 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:

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
}

此外,在JS中,与大多数人一样语言,你可以自由地忽略一个函数的返回值,这是非常重要的:

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:

(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

在某些非常低级别,返回被转换为某种跳转。如果一个函数真的没有返回 nothing ,那么就无法知道调用下一个函数的内容和时间,或者调用事件处理程序等。

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

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

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 always returns something, be it explicitly via a return statement, or implicitly. If a function returns implicitly, its return value will always be undefined.

这篇关于每个Javascript函数都必须返回一个值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆