JSDoc:返回对象结构


144

如何将返回的对象的结构告诉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,该信息可能比此页面更多。
莱瑟克

Answers:


263

使用@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};
}

2
谢谢。@return确实可以使用多个语句,但是它们会在输出中列出,就好像它们是多个返回值一样(状态为1的项目符号point - Object,然后为point.x - Number和的其他两个项目符号point.y - Number)。虽然我可以忍受,但是我想没有办法获得返回对象的压缩输出吗?或至少具有point.xpoint.y缩进的条目?
BlackWolf

1
是的,这似乎是最好的选择。我以为可能有一种没有命名返回实体的@typedef方法,但是就文档输出而言,这种方法是最清楚的一种,谢谢!
BlackWolf

groovy,删除了第一个选项,因为第二个选项简直是最好的。
BGerrissen

1
您最好在文档中添加@inner或定义类型global。+1
OnurYıldırım2016年

1
我一直都习惯@typedef {Object} Point。实际上,Point在PhpStorm中使用此两行形式的高亮显示消息为“未解析的变量或类型Point”。该@typedef文档支持这一点,但我不希望编辑这个答案,如果它是一个有效的变种。
David Harkness

22

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

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

17

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

/** 
 *  @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编译器时,出现警告:“无法实例化非构造函数”。我尝试将@constructor添加到函数中(在@return之上),但没有帮助。如果有人知道该如何解决,请告诉我。谢谢!
AndroidDev

2
@AndroidDev这是因为函数Point不是构造函数,要更改Pointthis.x = x; this.y = y;
Feirell '18

1
我看不到为什么我们需要在这里使用new的原因,为什么不只返回Point(x,y)?
CHANist

@CHANist,new语法是从创建实例constructor。没有new,上下文this将是全局上下文。您可以尝试创建实例,而无需new查看效果。
Akash)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.