【问题标题】:JSDoc: Return object structureJSDoc:返回对象结构
【发布时间】:2015-04-30 01:23:39
【问题描述】:

我如何告诉 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};
}

虽然解析成功,但生成的文档只是说明:

Returns: 
    The location of an event
    Type: Object

我正在开发一个 API,需要人们了解他们将返回的对象。这在 JSDoc 中可能吗?我正在使用 JSDoc3.3.0-beta1。

【问题讨论】:

  • 我知道@typedef 是一种解决方法/解决方案,但它不适用于文字对象似乎很奇怪。如果将来有人偶然发现这一点(就像我一样),我已经添加了一个问题github.com/jsdoc/jsdoc/issues/1678,它可能比这个页面有更多的信息。

标签: javascript documentation-generation jsdoc code-documentation jsdoc3


【解决方案1】:

单独定义你的结构using a @typdef:

/**
 * @typedef {Object} Point
 * @property {number} x - The X Coordinate
 * @property {number} y - The Y Coordinate
 */

并将其用作返回类型:

/**
 * 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};
}

【讨论】:

  • 谢谢。多个@return 语句确实有效,但它们在输出中列出,就好像它们是多个返回一样(一个项目符号表示point - Object,然后两个其他项目符号表示point.x - Numberpoint.y - Number)。虽然我可以忍受,但我想没有办法对返回的对象进行压缩输出?或者至少将point.xpoint.y 的条目缩进?
  • 是的,这似乎是最好的选择。我认为可能有一种方法可以有一个未命名的返回实体,但@typedef 方法在文档输出方面是最清晰的方法,谢谢!
  • groovy,删除了第一个选项,因为第二个选项是最好的。
  • 您最好在文档中添加@inner 或类型定义将具有global 范围。 +1
  • 我一直使用@typedef {Object} Point。事实上,使用这个两行表格在 PhpStorm 中突出显示 Point 并带有消息“未解析的变量或类型点”。 @typedef docs 支持这个,但如果它是一个有效的变体,我不想编辑这个答案。
【解决方案2】:

替代已经发布的建议,您可以使用以下格式:

/**
 * 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
  }
}

这将给出以下文档输出:

    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.

【讨论】:

    【解决方案3】:

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

    /** 
     *  @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);
    };
    

    【讨论】:

    • 当我这样做但使用 Google Closure Compiler 时,我收到警告:“无法实例化非构造函数”。我尝试将@constructor 添加到函数中(在@return 上方),但没有帮助。如果有人知道如何解决这个问题,请告诉我。谢谢!
    • @AndroidDev 这是因为函数Point 不是构造函数,要更改将Point 函数的主体替换为this.x = x; this.y = y;
    • 我看不出为什么我们需要在这里使用new,为什么不直接返回Point(x, y)?
    • @CHANist, new 语法是从constructor 创建一个实例。如果没有newthis 的上下文将是全局上下文。你可以尝试创建一个不带new的实例来看看效果。
    • 您忘记在您的Point JSDocs 上描述@returns
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多