正确的方式来loggingJSDoc中的开放式参数函数

假设你有如下的东西:

var someFunc = function() { // do something here with arguments } 

你如何正确地logging这个函数可以在JSDoc中取任意数量的参数? 这是我最好的猜测,但我不确定是否正确。

 /** * @param {Mixed} [...] Unlimited amount of optional parameters */ var someFunc = function() { // do something here with arguments } 

涉及到: PHP的 – 如何处理可变数量的参数

JSDoc规范和Google的Closure编译器这样做:

 @param {...number} var_args 

“数字”是预期的参数types。

这个完整的用法如下所示:

 /** * @param {...*} var_args */ function lookMaImVariadic(var_args) { // Utilize the `arguments` object here, not `var_args`. } 

请注意关于使用arguments (或arguments一些偏移量)来访问您的其他参数的注释。 var_args它只是用来发信号给你的IDE,这个参数确实存在。

ES6中的Rest参数可以进一步将实参arguments包含在提供的值中(因此不需要使用arguments ):

 /** * @param {...*} var_args */ function lookMaImES6Variadic(...var_args) { // Utilize the `var_args` array here, not `arguments`. } 

如何做到这一点现在在JSDoc文档中描述 ,它使用像Closure文档一样的省略号。

 @param {...<type>} <argName> <Argument description> 

您需要提供一个省略号后的types,但您可以使用*来描述接受任何内容,或使用| 分离多个可接受的types。 在生成的文档中,JSDoc将把这个参数描述为可重复的 ,就像它将可选参数描述为可选一样

在我的testing中,没有必要在实际的javascript函数定义中有一个参数,所以你的实际代码只能有空括号,即function whatever() { ... }

单一types:

 @param {...number} terms Terms to multiply together 

任何types(在下面的例子中,方括号中的平均items将被标记为可选和可重复的):

 @param {...*} [items] - zero or more items to log. 

在types列表周围需要多个types的括号,在开始之前使用省略号:

 @param {...(Person|string)} attendees - Meeting attendees, listed as either String names or {@link Person} objects 

这一段时间以来,我一直这么做。 以下是Google Closure Compiler的操作方法:

 /** * @param {...*} var_args */ function my_function(var_args) { // code that accesses the magic 'arguments' variable... } 

关键是给你的函数一个var_args参数(或者你在@param语句中调用它的任何东西),即使这个函数实际上并没有使用这个参数。

从JSDoc用户组 :

没有任何官方的方式,但是一个可能的解决scheme是这样的:

 /** * @param [...] Zero or more child nodes. If zero then ... otherwise .... */ 

方括号表示一个可选参数,而…(对我)表示“一些任意数字”。

另一种可能性是…

 /** * @param [arguments] The child nodes. */ 

无论哪种方式应该传达你的意思。

虽然(2007)有点过时,但是我还没有意识到任何更新鲜的东西。

如果您需要将参数typeslogging为“混合”,请使用{*} ,如@param {*} [arguments]