简体   繁体   English

jsdoc:如何使用括号表示法记录属性?

[英]jsdoc: how to document properties using bracket notation?

Given this rather silly function, how should documentation be written for "range" param?鉴于这个相当愚蠢的功能,应该如何为“范围”参数编写文档?

/**
* @param {number} value
 * @param {Object} range
 * @param {number} range['<']
 * @param {number} range['<=']
 * @param {number} range['>']
 * @param {number} range['>=']
 */
function testIsInRange(value, range) {
    var threshold;

    if ((threshold = range['<'], threshold !== undefined) && !(value < threshold)) {
        fail('Value must be less than ' + threshold);
    }

    if ((threshold = range['<='], threshold !== undefined) && !(value <= threshold)) {
        fail('Value must be less than or equal to ' + threshold);
    }

    if ((threshold = range['>'], threshold !== undefined) && !(value > threshold)) {
        fail('Value must be greater than ' + threshold);
    }

    if ((threshold = range['>='], threshold !== undefined) && !(value >= threshold)) {
        fail('Value must be greater than or equal to ' + threshold);
    }
}

Neither dot nor bracket notation seems to work.点和括号符号似乎都不起作用。 Property types (Object and number) are recognized, however their names are not.可以识别属性类型(对象和编号),但不能识别它们的名称。

Types can be specified as complex objects:类型可以指定为复杂对象:

/**
 * @param {number} value
 * @param {{'<': number, '<=': number, '>': number, '>=': number}} range
 */
function testIsInRange(value, range) {
    ...
}

Alternatively, destructuring can help, though it'll require renaming the parameters to non-symbolic names:或者,解构可以提供帮助,尽管它需要将参数重命名为非符号名称:

/**
 * @param {number} value
 * @param {number} lt
 * @param {number} lte
 * @param {number} gt
 * @param {number} gte
 */
function testIsInRange(value, {'<':lt, '<=':lte, '>':gt, '>=':gte}) {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用括号符号访问对象中的属性 - Accessing Properties in object using bracket notation 使用功能和括号符号将属性添加到对象 - Adding properties to an object using function and bracket notation 仅使用属性访问器(点符号或方括号符号),如何直接设置未定义的嵌套属性? - Using only property accessors (dot notation or bracket notation), how do I set undefined nested properties directly? 如何在jsdoc中记录函数和函数构造函数的属性? - How to document properties of a function and functional constructors in jsdoc? 如何使用括号符号在对象文字上创建嵌套属性? - How do I create nested properties on object literals using bracket notation? 不使用括号表示法防止Closure Compiler重命名属性 - Prevent Closure Compiler from renaming properties without using bracket notation 带方括号符号的“具有”范围和属性 - “with” scope and properties with square bracket notation 如何使用 jsDoc 正确记录类型为“Object”的 vue 属性? - How can I properly document vue properties with type "Object" using jsDoc? VS Code-如何使用JSDoc记录对象 - VS Code - how to document an object using JSDoc 如何使用 JSDoc 记录 function 返回的 function - How to document a function returned by a function using JSDoc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM