如何用可重复参数表示多种类型的参数?

时间:2016-02-29 05:01:30

标签: javascript jsdoc

/**
 * Returns the sum of all numbers passed to the function.
 * @example
 * sum([1,2,3])
 * //returns 6
 * sum(1,2,3)
 * //returns 6
 * @param {number[]|...number} params - ???
 */
function sum(params) {
    var args = params instanceof Array ? params : arguments;
    for (var i = 0, sum = 0; i < args.length; i++)
        sum += args[i];
    return sum;
}

'params'可以是数组或可重复的数字

但是“@param {number [] | ... number}”无效。

书面文件输出与“@param {number [] | number}”

没有区别

哪种方式可以表明这种情况?

1 个答案:

答案 0 :(得分:0)

诀窍是使用括号。我使用了以下内容。

* @param {...(object|string)} col - An optional set of columns to select.

针对您的参数名称进行了更新,此应该工作:

* @param {...(number[]|number)} params - ???

我找不到括号的任何文档,但在JSDoc 3.4.2下可以解决这个问题。

此评论:

/**
 * Select columns manually.
 * @param {...(object|string)} col - An optional set of columns to select.
 * Each argument can either be a fully-qualified column name in the form
 * &lt;table-alias&gt;.&lt;column-name&gt;, or an object with the following
 * properties below.  If no columns are specified, then all columns are
 * selected.
 * @param {string} col.column - The fully-qualified column name.
 * @param {string} col.mapTo - The name of the property that the column
 * should be mapped to in the resulting normalized object.  @param
 * @param {function} col.convert - A converter function that takes a single value
 * from the database and transforms it.  For example, a function that
 * converts a bit from the database to a boolean value.
 * @return {this}
 */

产生此文档:

enter image description here

希望有所帮助!