JSDoc: Return 对象结构
JSDoc: Return object structure
如何告诉 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.
单独定义结构using a @typdef:
/**
* @typedef {Object} Point
* @property {number} x - The X Coordinate
* @property {number} y - The Y Coordinate
*/
并将其用作 return 类型:
/**
* 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};
}
一个干净的解决方案是写一个 class 和 return 那。
/**
* @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);
};
替代已发布的建议,您可以使用此格式:
/**
* 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.
如何告诉 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.
单独定义结构using a @typdef:
/**
* @typedef {Object} Point
* @property {number} x - The X Coordinate
* @property {number} y - The Y Coordinate
*/
并将其用作 return 类型:
/**
* 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};
}
一个干净的解决方案是写一个 class 和 return 那。
/**
* @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);
};
替代已发布的建议,您可以使用此格式:
/**
* 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.