JSDoc:返回对象结构

Bla*_*olf 116 javascript documentation-generation code-documentation jsdoc jsdoc3

如何告诉JSDoc有关返回的对象的结构.我找到了@return {{field1: type, field2: type, ...}} description语法并试了一下:

/**
 * Returns a coordinate from a given mouse or touch event
 * @param  {TouchEvent|MouseEvent|jQuery.Event} e    
 *         A valid mouse or touch event or a jQuery event wrapping such an
 *         event. 
 * @param  {string} [type="page"]
 *         A string representing the type of location that should be
 *         returned. Can be either "page", "client" or "screen".
 * @return {{x: Number, y: Number}} 
 *         The location of the event
 */
var getEventLocation = function(e, type) {
    ...

    return {x: xLocation, y: yLocation};
}
Run Code Online (Sandbox Code Playgroud)

虽然这成功解析,但结果文档只是说明:

Returns: 
    The location of an event
    Type: Object
Run Code Online (Sandbox Code Playgroud)

我正在开发一个API,需要人们知道它们将被返回的对象.这在JSDoc中是否可行?我使用的是JSDoc3.3.0-beta1.

BGe*_*sen 218

单独定义:

/**
 * @typedef {Object} Point
 * @property {number} x - The X Coordinate
 * @property {number} y - The Y Coordinate
 */
Run Code Online (Sandbox Code Playgroud)

并使用:

/**
 * Returns a coordinate from a given mouse or touch event
 * @param  {TouchEvent|MouseEvent|jQuery.Event} e    
 *         A valid mouse or touch event or a jQuery event wrapping such an
 *         event. 
 * @param  {string} [type="page"]
 *         A string representing the type of location that should be
 *         returned. Can be either "page", "client" or "screen".
 * @return {Point} 
 *         The location of the event
 */
var getEventLocation = function(e, type) {
    ...

    return {x: xLocation, y: yLocation};
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.多个`@ return`语句确实有效,但是它们在输出中列出就像它们是多个返回一样(一个项目符号点表示`point-Object`然后是另外两个要点`point.x - Number`和`point .y - 数字`).虽然我可以忍受这种情况,但我认为没有办法获得返回对象的精简输出?或者至少有'point.x`和`point.y`的条目缩进? (2认同)
  • 您最好添加 `@inner` 或类型定义将在文档中具有 `global` 范围。+1 (2认同)

moo*_*ses 14

一个干净的解决方案是编写一个类并返回它.

/** 
 *  @class Point
 *  @type {Object}
 *  @property {number} x The X-coordinate.
 *  @property {number} y The Y-coordinate.
 */
function Point(x, y) {
  return {
        x: x,
        y: y
    };
}

/**
 * @returns {Point} The location of the event.
 */
var getEventLocation = function(e, type) {
    ...
    return new Point(x, y);
};
Run Code Online (Sandbox Code Playgroud)

  • @AndroidDev这是因为函数`Point`不是构造函数,更改用`this.x = x替换`Point`函数的主体; this.y = y;` (2认同)

Mat*_*lly 12

除了已经发布的建议之外,您还可以使用以下格式:

/**
 * Get the connection state.
 *
 * @returns {Object} connection The connection state.
 * @returns {boolean} connection.isConnected Whether the authenticated user is currently connected.
 * @returns {boolean} connection.isPending Whether the authenticated user's connection is currently pending.
 * @returns {Object} connection.error The error object if an error occurred.
 * @returns {string} connection.error.message The error message.
 * @returns {string} connection.error.stack The stack trace of the error.
 */
getConnection () {
  return {
    isConnected: true,
    isPending: false,
    error
  }
}
Run Code Online (Sandbox Code Playgroud)

这将提供以下文档输出:

    Get the connection state.

    getConnection(): Object

    Returns
    Object: connection The connection state.
    boolean: connection.isConnected Whether the authenticated user is currently connected.
    boolean: connection.isPending Whether the authenticated users connection is currently pending.
    Object: connection.error The error object if an error occurred.
    string: connection.error.message The error message.
    string: connection.error.stack The stack trace of the error.
Run Code Online (Sandbox Code Playgroud)