在JSDoc中记录开放式参数函数的正确方法 [英] Correct way to document open-ended argument functions in JSDoc

查看:507
本文介绍了在JSDoc中记录开放式参数函数的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有以下内容:

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

你怎么能正确记录这个函数可以接受任何数字JSDoc中的参数?这是我最好的猜测,但我不确定它是否正确。

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
}

相关: php - 如何记录可变数量的参数

推荐答案

JSDoc规范 Google的Closure Compiler 这样:

@param {...number} var_args

其中number是预期参数的类型。

Where "number" is the type of arguments expected.

完全使用这个,如下所示:

The complete usage of this, then, would look like the following:

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

注意关于利用参数的评论(或参数的一些偏移量)来访问您的其他参数。 var_args 它只是用来向你的IDE发出信号确实存在参数确实存在。

Note the comment about utilizing arguments (or some offset of arguments) to access your additional arguments. var_args it just used to signal to your IDE that the argument does indeed exist.

ES6中的其余参数可以将真实参数更进一步包含在内值(因此不再需要使用参数):

Rest parameters in ES6 can take the real parameter one step further to encompass provided values (so no more use of arguments is necessary):

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

这篇关于在JSDoc中记录开放式参数函数的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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