如何记录具有已知参数类型的可变长度的参数列表?

发布于 2024-11-17 09:20:58 字数 1245 浏览 5 评论 0原文

相关:在 JSDoc 中记录开放式参数函数的正确方法

我有一个通过访问 < 来接受多个数组的函数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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-11-24 09:20:58

您的函数并不是真正的可变参数,您应该将其签名更改为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;
}

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

/**
 * @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
 */

And you'd call it like

modify_text('search', 'replacement', {
    catch_errors: function(e) {

    },
    escape: 'someEscape'

});

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

/**
 * 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;
}
老子叫无熙 2024-11-24 09:20:58

除非您受到其他 API 的限制,否则我建议的第一件事是:除了您打算迭代的数据集合之外,不要使用数组。

可能是这里最好的方法将重构该函数,使其采用三个参数或某种参数对象。例如:

/**
 * @param {String} searchTerm
 * @param {String} replacementText
 * @param {Object} replacementOpts (optional) An object containing the replacement
 * options; optional values in the object include:<ul>
   <li>catch_errors {Type} description text...</li>
   <li>escape {Type} description text...</li></ul>
 */

我强烈建议不要使用数组(再次强调:“除非您受到某些超出您控制范围的 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:

/**
 * @param {String} searchTerm
 * @param {String} replacementText
 * @param {Object} replacementOpts (optional) An object containing the replacement
 * options; optional values in the object include:<ul>
   <li>catch_errors {Type} description text...</li>
   <li>escape {Type} description text...</li></ul>
 */

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文