JsDoc中如何返回void?
JsDoc中是否有指定的方法或函数声明返回void?目前我认为 void
是默认返回值,其他返回值必须特别提供:
/**
* @return {Integer} The identifier for ...
*/
Is there a specified way to declare a method or a function to return void in JsDoc? Currently I am in the belief that void
is the default return value, and other return values must be specifically provided:
/**
* @return {Integer} The identifier for ...
*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Closure Compiler
根据 Google Closure Compiler 的文档,如果没有返回任何内容,则应省略 @return 注释。
来源: https://developers. google.com/closure/compiler/docs/js-for-compiler#tags
jsdoc-toolkit
但是,进一步的文档还指出 returnType 和 returnDescription 是可选参数。
来源: https://code.google.com/ p/jsdoc-toolkit/wiki/TagReturns
摘要
您可以省略返回注释,也可以将其包含在不带任何参数的情况下。
Closure Compiler
According to the documentation of Google's Closure Compiler if nothing is being returned, the @return annotation should be omitted.
Source: https://developers.google.com/closure/compiler/docs/js-for-compiler#tags
jsdoc-toolkit
However further documentation also states that the returnType and returnDescription are optional parameters.
Source: https://code.google.com/p/jsdoc-toolkit/wiki/TagReturns
Summary
You could either leave out the return annotation or include it without any parameters.
我不相信您必须从 JsDoc 中的一组类型中进行选择...您可以使用您想要的任何类型名称(大括号表示它是一种类型),因此您可以简单地执行以下操作:
虽然,这可能更正确对于 JavaScript:
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:
Although, this is probably more correct for JavaScript:
查看他们使用的 ESlint 文档
@returns {void}
来源:http://eslint.org/docs /rules/valid-jsdoc
由于我需要为每个函数提供一个
@returns
来通过测试,以便为某些项目推送代码,这在我的情况下是必需的。Looking at the ESlint docs they use
@returns {void}
Source: http://eslint.org/docs/rules/valid-jsdoc
Since I need to provide an
@returns
for each function to pass tests in order to push code for certain projects this is required in my case.我发现这比省略
@returns
:甚至
只是一个想法更明确。
I find this less ambiguous than leaving out the
@returns
:or even
Just an idea.
为了帮助在其他答案之间做出决定,显式编写的
@return {void}
有一个好处。虽然@return可以省略,但对于文档编码员来说,看到@return {void}可以帮助记忆。它告诉编码人员返回文档已编写。如果没有编写@return
,那么编码人员可能需要检查他是否忘记编写返回值文档或者该函数是否确实没有返回任何内容。当然,返回值文档可能已经过时。这是编码人员无法直接从文档中得知的,需要阅读代码来验证。尽管如此,看到写入的返回值可以让编码人员确信文档是正确的,而不仅仅是匆忙编写的。
To help to decide between the other answers, there is one benefit in explicitly written
@return {void}
. Although@return
can be omitted, for a documenting coder, seeing the@return {void}
works as a memory aid. It tells the coder that the return documentation is written. If there is no@return
written, then the coder might need to inspect did he forget to write the return value documentation or does the function truly return nothing.Of course, the return value documentation might be outdated. That is something the coder cannot directly know from the docs and needs to read the code to verify. Still, seeing the return value written gives the coder a bit of certainty that the documentation is correct and not just hastily written.