Javascript JSDoc:返回对象结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28763257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSDoc: Return object structure
提问by BlackWolf
How can I tell JSDoc about the structure of an object that is returned. I have found the @return {{field1: type, field2: type, ...}} descriptionsyntax and tried it:
如何告诉 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};
}
While this parses successfully, the resulting documentation simply states:
虽然这解析成功,但生成的文档只是说明:
Returns:
The location of an event
Type: Object
I am developing an API and need people to know about the object that they will get returned. Is this possible in JSDoc? I am using JSDoc3.3.0-beta1.
我正在开发一个 API,需要人们了解他们将返回的对象。这在 JSDoc 中可能吗?我正在使用 JSDoc3.3.0-beta1。
回答by BGerrissen
Define your structure separately using a @typdef:
使用 @typdef单独定义您的结构:
/**
* @typedef {Object} Point
* @property {number} x - The X Coordinate
* @property {number} y - The Y Coordinate
*/
And use it as the return type:
并将其用作返回类型:
/**
* 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};
}
回答by Matt Pengelly
An alternative to the suggestions already posted, you can use this format:
已经发布的建议的替代方案,您可以使用以下格式:
/**
* 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
}
}
which will give the following documentation output:
这将提供以下文档输出:
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.
回答by mooses
A clean solution is to write a class and return that.
一个干净的解决方案是编写一个类并返回它。
/**
* @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);
};

