JSDoc: structure of the returned object

How can I tell JSDoc about the structure of the returned object. I found the syntax @return {{field1: type, field2: type, ...}} description 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 has been successfully analyzed, the final documentation simply says:

 Returns: The location of an event Type: Object 

I am developing an API and need to let people know about the object they will receive. Is this possible in JSDoc? I am using JSDoc3.3.0-beta1.

+95
javascript documentation-generation jsdoc code-documentation jsdoc3
Feb 27 '15 at 10:49
source share
2 answers

Define separately:

 /** * @typedef {Object} Point * @property {number} x - The X Coordinate * @property {number} y - The Y Coordinate */ 

And use:

 /** * 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}; } 
+177
Feb 27 '15 at 11:06
source share

The pure solution is to write a class and return it.

 /** * @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); }; 
+12
Apr 21 '16 at 4:26
source share



All Articles