在 JSDoc 中记录开放式参数函数的正确方法
假设您有如下内容:
var someFunc = function() {
// do something here with arguments
}
您如何正确记录该函数可以在 JSDoc 中接受任意数量的参数?这是我最好的猜测,但我不确定它是否正确。
/**
* @param {Mixed} [...] Unlimited amount of optional parameters
*/
var someFunc = function() {
// do something here with arguments
}
Let's say you have something like the following:
var someFunc = function() {
// do something here with arguments
}
How would you correctly document that this function can take any number of arguments in JSDoc? This is my best guess, but I'm not sure it's correct.
/**
* @param {Mixed} [...] Unlimited amount of optional parameters
*/
var someFunc = function() {
// do something here with arguments
}
Related to: php - How to doc a variable number of parameters
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JSDoc 规范 和 Google 的 Closure 编译器 执行以下操作是这样的:
其中“number”是期望的参数类型。
那么,它的完整用法如下所示:
请注意有关利用
参数
(或参数
的一些偏移量)来访问附加参数的注释。var_args
它只是用来向您的 IDE 发出信号,表明该参数确实存在。ES6 中的 Rest 参数 可以采用真实参数进一步包含提供的值(因此不再需要使用参数):
The JSDoc specs and Google's Closure Compiler do it this way:
Where "number" is the type of arguments expected.
The complete usage of this, then, would look like the following:
Note the comment about utilizing
arguments
(or some offset ofarguments
) to access your additional arguments.var_args
it just used to signal to your IDE that the argument does indeed exist.Rest parameters in ES6 can take the real parameter one step further to encompass provided values (so no more use of
arguments
is necessary):JSDoc 文档中现在描述了如何执行此操作,并且它使用像 Closure 文档一样省略号。
您需要在省略号后面提供一个类型,但您可以使用
*
来描述接受任何内容,或使用|
来分隔多个可接受的类型。在生成的文档中,JSDoc 会将此参数描述为可重复,就像它将可选参数描述为可选一样。在我的测试中,实际的 javascript 函数定义中不需要有参数,因此您的实际代码可以只包含空括号,即
functionwhatever() { ... }
。单一类型:
任何类型(在下面的示例中,方括号意味着
items
将被标记为可选和可重复):多个类型需要在类型列表两边加上括号,并在左括号之前加上省略号:
How to do this is now described in the JSDoc documentation, and it uses an ellipsis like the Closure docs do.
You need to supply a type to go after the ellipsis, but you can use a
*
to describe accepting anything, or use the|
to separate multiple acceptable types. In the generated documentation JSDoc will describe this argument as repeatable, in the same way it describes optional arguments as optional.In my testing there was no need to have an argument in the actual javascript function definition, so your actual code can just have empty parentheses, i.e.
function whatever() { ... }
.Single type:
Any type (in the example below, the square brackets mean
items
will get tagged as both optional and repeatable):Multiple types need parentheses around the type list, with the ellipsis before the opening paren:
来自 JSDoc 用户群组:
虽然它有点过时了(2007 年),但我不知道有什么更新的内容。
如果您需要将参数类型记录为“混合”,请使用
{*}
,如@param {*} [arguments]
中所示。From the JSDoc users group:
It's a bit dated, though (2007), but I'm not aware of anything more current.
If you need to document the param type as 'mixed', use
{*}
, as in@param {*} [arguments]
.我为此困惑了相当长一段时间。以下是如何使用 Google Closure Compiler 执行此操作:
关键是为您的函数提供一个
var_args
参数(或您在@param
语句中调用的任何名称),即使该函数实际上并没有使用该参数。I futzed with this for quite some time. Here's how to do it with Google Closure Compiler:
The key is to give your function a
var_args
parameter (or whatever you call it in your@param
statement) even though the function doesn't actually use that parameter.