javascript - JSDoc - 如何使用通用键名记录对象?

标签 javascript ecmascript-6 documentation jsdoc

<分区>

我需要用 JSDoc 记录一个 ES6 类,该类接受一个对象,该对象具有以键名作为人名的属性,因此键名几乎可以是任何字符串,没有任何预定义。所以对象的结构应该是这样的:

{
    "Name of person": {
        "age": 31,
        "hobby": "Tennis"
    },
    "Name of another person": {
        "age": 29,
        "hobby": "Running"
    }
}

所以每个人的名字都是一个键,但它可以是任何东西,它不是预定义的。我要记录的示例:

class ExampleClass {
    /**
     * Creates an instance of ExampleClass
     * @param {Object} peopleObj            - Contains information about people.
     * @param {String} peopleObj.name       - The name of the person. <----- how should this be documented?
     * @param {Number} peopleObj.name.age   - The age of the person.
     * @param {String} peopleObj.name.hobby - The hobby of the person.
     * @memberof ExampleClass
     */
    constructor(peopleObj) {
        // Do stuff
    }
}

我觉得如果我输入“peopleObj.name”就意味着键应该是“name”而不是任何你喜欢的名字。那么我如何通过让用户知道他可以在那里插入他喜欢的任何名称来记录这一点?


编辑

对于任何想知道的人,这就是我最终记录的方式(由于有人关闭了这个问题,无法将其添加为答案)。

/**
* Information about a single person.
* @typedef Person
* @type {object}
* @property {number} age   - The age of the person.
* @property {string} hobby - The hobby of the person.
*/
class ExampleClass {
    /**
     * Creates an instance of ExampleClass.
     * @param {Object.<string, Person>} peopleObj - Information about people as {@link Person}
     *                                              objects where the key is their name.
     * @memberof ExampleClass
     */
    constructor(peopleObj) {
        // Do stuff
    }
}

最佳答案

您所描述的内容包含在 @type 中JSDoc 文档。

对象应该记录如下:

/**
 * @typedef Person
 * @type {Object}
 * @property {number} age - the person's age
 * @property {string} hobby - the person's hobby
 */

/** 
 * ExampleClass
 */
class ExampleClass {

    /**
     * Creates a dictionary of people
     * @param {Object.<string, Person>} peopleObj - an object with names as keys and Person objects as values. 
     * @memberof ExampleClass
     */
     constructor(peopleObj) {}
}

关于javascript - JSDoc - 如何使用通用键名记录对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44948251/

相关文章:

javascript - 使用 jquery 检测 URL 并更改 HTML

javascript - 在 JSP 页面上显示 Javascript 获取的列表值

javascript - 需要表格来填充搜索栏的搜索结果

javascript - 如何计算数组中相同项目的数量并将计数分配给对象中的特定键?

maven - 将代码格式化为 Doxia Apt 格式

documentation - Dart - 自托管包

javascript - Angular2 中 IFrame 的跨域问题

javascript - 具有 HOC 的命名导出

javascript - 是否有糖与可选链接/无效合并来防止 Array.map 出现类型错误?

ruby - 获取 Ruby 文档的最快方法是什么?