如何记录具有已知参数类型的可变长度的参数列表?
我有一个通过访问 < 来接受多个数组的函数code>arguments 变量:
/**
* @param options An object containing options
* @param [options.bind] blablabla (optional)
*/
function modify_function (options) {
for (var i=1; i<arguments.length; i++) {
// ...
}
}
现在,我知道除了 options
之外的每个参数都是一个包含值得记录的值的数组:
[search_term, replacement, options]
我不考虑将(冗长的)描述放在变量参数中 线。
@param {...} 包含搜索词、替换词及其选项的数组;索引 0:函数内的搜索项; 1:替换文字; 2:可选选项(catch_errors:捕获错误并记录它,escape:在替换文本中转义美元,pos:“L”用于将替换放在搜索词之前,“R”用于将其放置在搜索词之后) 不是一个可读的解决方案,并且类型不可见。
有没有办法记录变量参数的类型和值?
@param {...[]} An array with search terms, replacements and its options
@param {...[0]} The search term within the function
@param {...[1]} The replacement text
@param {...[2]} An optional object with obtions for the replacement
@param {...[2].catch_errors} catches errors and log it
@param {...[2].escape} etc...
上面看起来很难看,但它应该让您了解我想要实现的目标:
- 记录变量参数的类型(在本例中是数组)
- 记录该数组的值
- 记录该数组内对象的属性
为了懒惰,我使用了数组而不是对象。随时欢迎其他建议。
Related: Correct way to document open-ended argument functions in JSDoc
I've a function that accepts multiple arrays by accessing the arguments
variable:
/**
* @param options An object containing options
* @param [options.bind] blablabla (optional)
*/
function modify_function (options) {
for (var i=1; i<arguments.length; i++) {
// ...
}
}
Now, I know that each argument besides options
is an array containing values that are worth documenting:
[search_term, replacement, options]
I'm not considering putting the (lengthy) description in the variable parameter line.
@param {...} An array with search terms, replacements and its options; index 0: the search term within the function; 1: the replacement text; 2: optional options (catch_errors: catches errors and log it, escape: escape dollars in the replacement text, pos: "L" for placing the replacement before the search term, "R" for placing it after)
Not a readable solution and the type is not visible.
Is there a way to document the types and values of a variable parameter?
@param {...[]} An array with search terms, replacements and its options
@param {...[0]} The search term within the function
@param {...[1]} The replacement text
@param {...[2]} An optional object with obtions for the replacement
@param {...[2].catch_errors} catches errors and log it
@param {...[2].escape} etc...
The above looks ugly, but it should give you an idea of what I'm trying to achieve:
- document the type of a variable parameter (in this case an Array)
- document the values of this array
- document the properties of an object inside this array
For laziness, I've used an array instead of a object. Other suggestions are always welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的函数并不是真正的可变参数,您应该将其签名更改为foundrama 建议的内容。除了 JSDoc 的语法比 Foundrama 建议的好一点之外
,你可以这样称呼它:
如果你确实有 varargs 风格的情况,它应该是一个相同类型的变量,可以在参数列表的末尾传递,我将其记录如下,虽然它不是 JSDoc 标准,但它是 Google 在其文档中使用的标准
Your function is not truly variable arguments, you should just change its signature to what foundrama suggested. Except that JSDoc has syntax a little better than foundrama suggested
And you'd call it like
If you really do have a case for varargs style, it should be a variable of the same type that can be passed at the end of the parameter list, I document it like the following, though it's not a JSDoc standard, it's what Google uses with their documentation
除非您受到其他 API 的限制,否则我建议的第一件事是:除了您打算迭代的数据集合之外,不要使用数组。
可能是这里最好的方法将重构该函数,使其采用三个参数或某种参数对象。例如:
我强烈建议不要使用数组(再次强调:“除非您受到某些超出您控制范围的 API 的限制),因为它会变得脆弱并且最终会有点令人困惑。
Unless you are restricted by some other API, then the first thing I would suggest would be: don't use an array for anything except a data collection that you intend to iterate over.
Probably the best approach here would be to re-factor the function such that it takes three parameters or else some kind of param object. For example:
I strongly recommend against using the array (again: "unless you're bounded by some API outside of your control) as it will prove both brittle and ultimately a little confusing.