小编典典

JSDoc:返回对象结构

all

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


阅读 81

收藏
2022-06-18

共1个答案

小编典典

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