Monday, May 20, 2024
140
rated 0 times [  147] [ 7]  / answers: 1 / hits: 124112  / 9 Years ago, fri, february 27, 2015, 12:00:00

How can I tell JSDoc about the structure of an object that is returned. I have found the @return {{field1: type, field2: type, ...}} description syntax and tried it:



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


More From » documentation-generation

 Answers
361

Define your structure separately using a @typedef:


/**
* @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};
}

[#67646] Wednesday, February 25, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ignacio

Total Points: 467
Total Questions: 128
Total Answers: 79

Location: Luxembourg
Member since Tue, Mar 14, 2023
1 Year ago
ignacio questions
;