繁体   English   中英

如何在jsdoc中描述“对象”参数?

[英]How to describe "object" arguments in jsdoc?

// My function does X and Y.
// @params {object} parameters An object containing the parameters
// @params {function} callback The callback function
function(parameters, callback) {
}

但是我该如何描述参数对象的结构呢? 例如它应该是这样的:

{
  setting1 : 123, // (required, integer)
  setting2 : 'asdf' // (optional, string)
}

@param wiki 页面


带属性的参数

如果期望参数具有特定属性,则可以在该参数的 @param 标记之后立即记录该属性,如下所示:

 /**
  * @param userInfo Information about the user.
  * @param userInfo.name The name of the user.
  * @param userInfo.email The email of the user.
  */
 function logIn(userInfo) {
        doLogIn(userInfo.name, userInfo.email);
 }

曾经有一个@config 标签紧跟在相应的@param 之后,但它似乎已被弃用(示例here )。

到目前为止,有 4 种不同的方法可以将对象记录为参数/类型。 每个都有自己的用途。 但是,其中只有 3 个可用于记录返回值。

对于具有一组已知属性的对象(变体 A)

/**
 * @param {{a: number, b: string, c}} myObj description
 */

此语法非常适合仅用作此函数的参数且不需要对每个属性进行进一步描述的对象。 也可以用于@returns

对于具有一组已知属性的对象(变体 B)

非常有用的是带有属性语法的参数

/**
 * @param {Object} myObj description
 * @param {number} myObj.a description
 * @param {string} myObj.b description
 * @param {} myObj.c description
 */

此语法非常适用于仅用作此函数的参数且需要对每个属性进行进一步描述的对象。 这不能用于@returns

对于将在源中多个点使用的对象

在这种情况下, @typedef非常方便。 您可以在源代码中的某个位置定义类型,并将其用作@param@returns或其他可以使用类型的 JSDoc 标记的类型。

/**
 * @typedef {Object} Person
 * @property {string} name how the person is called
 * @property {number} age how many years the person lived
 */

然后你可以在@param标签中使用它:

/**
 * @param {Person} p - Description of p
 */

或者在@returns

/**
 * @returns {Person} Description
 */

对于值都是相同类型的对象

/**
 * @param {Object.<string, number>} dict
 */

第一种类型(字符串)记录了键的类型,在 JavaScript 中它总是一个字符串,或者至少总是被强制转换为一个字符串。 第二种类型(数字)是值的类型; 这可以是任何类型。 此语法也可用于@returns

资源

可以在此处找到有关文档类型的有用信息:

https://jsdoc.app/tags-type.html

PS:

要记录一个可选值,您可以使用[]

/**
 * @param {number} [opt_number] this number is optional
 */

或者:

/**
 * @param {number|undefined} opt_number this number is optional
 */

我看到已经有关于@return 标签的答案,但我想提供更多详细信息。

首先,官方的 JSDoc 3 文档没有给我们任何关于自定义对象的 @return 的示例。 请参阅https://jsdoc.app/tags-returns.html 现在,让我们看看在某些标准出现之前我们可以做什么。

  • 函数返回动态生成键的对象。 示例: {1: 'Pete', 2: 'Mary', 3: 'John'} 通常,我们在for(var key in obj){...}的帮助下迭代这个对象。

    根据https://google.github.io/styleguide/javascriptguide.xml#JsTypes可能的 JSDoc

     /** * @return {Object.<number, string>} */ function getTmpObject() { var result = {} for (var i = 10; i >= 0; i--) { result[i * 3] = 'someValue' + i; } return result }
  • 函数返回对象,其中键是已知常量。 示例: {id: 1, title: 'Hello world', type: 'LEARN', children: {...}} 我们可以轻松访问此对象的属性: object.id

    根据https://groups.google.com/forum/#!topic/jsdoc-users/TMvUedK9tC4可能的 JSDoc

    • 假装。

       /** * Generate a point. * * @returns {Object} point - The point generated by the factory. * @returns {number} point.x - The x coordinate. * @returns {number} point.y - The y coordinate. */ var pointFactory = function (x, y) { return { x:x, y:y } }
    • 光猪六壮士。

       /** @class generatedPoint @private @type {Object} @property {number} x The x coordinate. @property {number} y The y coordinate. */ function generatedPoint(x, y) { return { x:x, y:y }; } /** * Generate a point. * * @returns {generatedPoint} The point generated by the factory. */ var pointFactory = function (x, y) { return new generatedPoint(x, y); }
    • 定义一个类型。

       /** @typedef generatedPoint @type {Object} @property {number} x The x coordinate. @property {number} y The y coordinate. */ /** * Generate a point. * * @returns {generatedPoint} The point generated by the factory. */ var pointFactory = function (x, y) { return { x:x, y:y } }

    根据https://google.github.io/styleguide/javascriptguide.xml#JsTypes

    • 记录类型。

       /** * @return {{myNum: number, myObject}} * An anonymous type with the given type members. */ function getTmpObject() { return { myNum: 2, myObject: 0 || undefined || {} } }

对于@return标签使用{{field1: Number, field2: String}} ,请参阅: http : //wiki.servoy.com/display/public/DOCS/Annotating+JavaScript+using+JSDoc

如果期望参数具有特定属性,则可以通过提供额外的@param标记来记录该属性。 例如,如果希望员工参数具有姓名和部门属性,您可以按如下方式记录它:

/**
 * Assign the project to a list of employees.
 * @param {Object[]} employees - The employees who are responsible for the project.
 * @param {string} employees[].name - The name of an employee.
 * @param {string} employees[].department - The employee's department.
 */
function(employees) {
    // ...
}

如果一个参数在没有明确名称的情况下被解构,您可以为该对象提供一个合适的名称并记录其属性。

/**
 * Assign the project to an employee.
 * @param {Object} employee - The employee who is responsible for the project.
 * @param {string} employee.name - The name of the employee.
 * @param {string} employee.department - The employee's department.
 */
Project.prototype.assign = function({ name, department }) {
    // ...
};

资料来源: JSDoc

这些情况有一个新的@config标签。 它们链接到前面的@param

/** My function does X and Y.
    @params {object} parameters An object containing the parameters
    @config {integer} setting1 A required setting.
    @config {string} [setting2] An optional setting.
    @params {MyClass~FuncCallback} callback The callback function
*/
function(parameters, callback) {
    // ...
};

/**
 * This callback is displayed as part of the MyClass class.
 * @callback MyClass~FuncCallback
 * @param {number} responseCode
 * @param {string} responseMessage
 */

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM