首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JSDoc:返回对象结构

JSDoc:返回对象结构
EN

Stack Overflow用户
提问于 2015-02-27 18:49:18
回答 3查看 92.1K关注 0票数 200

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

代码语言:javascript
复制
/**
 * 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};
}

虽然这个解析成功,但生成的文档只是简单地说明:

代码语言:javascript
复制
Returns: 
    The location of an event
    Type: Object

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-02-27 19:06:45

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

代码语言:javascript
复制
/**
 * @typedef {Object} Point
 * @property {number} x - The X Coordinate
 * @property {number} y - The Y Coordinate
 */

并将其用作返回类型:

代码语言:javascript
复制
/**
 * 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};
}
票数 337
EN

Stack Overflow用户

发布于 2019-05-30 02:17:24

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

代码语言:javascript
复制
/**
 * 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
  }
}

这将提供以下文档输出:

代码语言:javascript
复制
    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.
票数 38
EN

Stack Overflow用户

发布于 2016-04-21 12:26:36

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

代码语言:javascript
复制
/** 
 *  @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);
};
票数 24
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28763257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档