403
// 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)
}
4

6 回答 6

533

@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 之后,但它似乎已被弃用(这里的示例)。

于 2011-06-23T21:19:49.240 回答
291

到目前为止,有 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
 */
于 2015-07-22T20:32:30.747 回答
145

我看到已经有关于 @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 || {}
          }
      }
      
于 2013-10-11T20:45:25.740 回答
21

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

于 2013-03-01T11:06:46.267 回答
14

如果期望参数具有特定属性,则可以通过提供附加@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

于 2020-03-05T02:54:03.490 回答
0

这些案例有一个新@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
 */
于 2013-08-22T17:02:32.353 回答