相关:
Correct way to document open-ended argument functions in JSDoc
我有一个通过访问参数变量接受多个数组的函数:
/**
* @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 {…} 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.
有没有办法记录变量参数的类型和值?
@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...
上面看起来很难看,但它应该让你知道我想要实现的目标:
>记录变量参数的类型(在本例中为Array)
>记录此数组的值
>记录此数组中对象的属性
对于懒惰,我使用了数组而不是对象.其他建议随时欢迎.
解决方法
你的函数不是真正可变的参数,你应该将它的签名改为foundrama建议的内容.除了JSDoc的语法比foundrama建议的好一点
/**
* @param {String} searchTerm
* @param {String} replacementText
* @param {Object} opts (optional) An object containing the replacement options
* @param {Function} opts.catch_errors Description text
* @param {Event} opts.catch_errors.e The name of the first parameter
* passed to catch_errors
* @param {Type} opts.escape Description of options
*/
你会称之为
modify_text('search','replacement',{
catch_errors: function(e) {
},escape: 'someEscape'
});
如果你真的有varargs样式的情况,它应该是一个可以在参数列表末尾传递的相同类型的变量,我将其记录如下,尽管它不是JSDoc标准,它是Google使用的与他们的文件
/**
* Sums its parameters
* @param {...number} var_args Numbers to be added together
* @return number
*/
function sum(/* num,num,... */) {
var sum = 0;
for (var i =0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}