Javascript 如何在 JsDoc 中返回 void?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4759175/
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-08-23 13:57:28  来源:igfitidea点击:

How to return void in JsDoc?

javascriptidejsdoc

提问by Tower

Is there a specified way to declare a method or a function to return void in JsDoc? Currently I am in the belief that voidis the default return value, and other return values must be specifically provided:

在 JsDoc 中是否有指定方法或函数来声明返回 void 的方法?目前我认为这void是默认返回值,必须特别提供其他返回值:

/**
 * @return {Integer} The identifier for ...
 */

回答by Brent Robinson

Closure Compiler

闭包编译器

According to the documentation of Google's Closure Compiler if nothing is being returned, the @return annotation should be omitted.

根据 Google's Closure Compiler 的文档,如果没有返回任何内容,则应省略 @return 注释。

If there is no return value, do not use a @return tag.

如果没有返回值,不要使用@return 标签。

Source:https://developers.google.com/closure/compiler/docs/js-for-compiler#tags

来源:https : //developers.google.com/closure/compiler/docs/js-for-compiler#tags

jsdoc-toolkit

jsdoc-工具包

However further documentation also states that the returnType and returnDescription are optional parameters.

然而,进一步的文档还指出 returnType 和 returnDescription 是可选参数。

returnType - Optional: the type of the return value.

returnDescription - Optional: any additional description.

returnType - 可选:返回值的类型。

returnDescription - 可选:任何附加说明。

Source:https://code.google.com/p/jsdoc-toolkit/wiki/TagReturns

来源:https : //code.google.com/p/jsdoc-toolkit/wiki/TagReturns

Summary

概括

You could either leave out the return annotation or include it without any parameters.

您可以省略返回注释,也可以不带任何参数包含它。

回答by David Tang

I don't believe you have to choose from a set of types in JsDoc... you can use any type name you wish (the curly braces indicate it's a type), so you can simply do:

我不相信您必须从 JsDoc 中的一组类型中进行选择……您可以使用任何您希望的类型名称(大括号表示它是一种类型),因此您可以简单地执行以下操作:

@return {Void}

Although, this is probably more correct for JavaScript:

虽然,这对于 JavaScript 来说可能更正确:

@return {undefined}

回答by primetimejas

Looking at the ESlint docs they use @returns {void}

查看他们使用的 ESlint 文档 @returns {void}

Source: http://eslint.org/docs/rules/valid-jsdoc

来源:http: //eslint.org/docs/rules/valid-jsdoc

Since I need to provide an @returnsfor each function to pass tests in order to push code for certain projects this is required in my case.

由于我需要@returns为每个函数提供一个以通过测试以便为某些项目推送代码,因此在我的情况下这是必需的。

回答by Akseli Palén

If you need to say out loud that nothing is returned, you can say that in the free-form description. This is useful to clarify situations where a user might expect something to be returned. Of course proper naming of the function and the parametersshould alone make the expected return type apparent, but it might not always be possible.

如果您需要大声说不返回任何内容,可以在自由格式说明中说。这对于澄清用户可能期望返回某些内容的情况很有用。当然,函数和参数的正确命名应该单独使预期的返回类型显而易见,但这可能并不总是可能的。

/**
 * This is a funny function. Returns nothing.
 * @param {string} a joke.
 */
var funny = function (joke) {
  console.log(joke);
};