Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to document anonymous objects and functions with jsdoc

Edit: This is technically a 2 part question. I've chosen the best answer that covers the question in general and linked to the answer that handles the specific question.

What is the best way to document anonymous objects and functions with jsdoc?

/**  * @class {Page} Page Class specification  */ var Page = function() {      /**      * Get a page from the server      * @param {PageRequest} pageRequest Info on the page you want to request      * @param {function} callback Function executed when page is retrieved      */     this.getPage = function(pageRequest, callback) {     };  }; 

Neither the PageRequest object or the callback exist in code. They will be provided to getPage() at runtime. But I would like to be able to define what the object and function are.

I can get away with creating the PageRequest object to document that:

/**  * @namespace {PageRequest} Object specification  * @property {String} pageId ID of the page you want.  * @property {String} pageName Name of the page you want.  */ var PageRequest = {     pageId : null,     pageName : null }; 

And that's fine (though I'm open to better ways to do this).

What is the best way to document the callback function? I want to make it know in the document that, for example, the callback function is in the form of:

callback: function({PageResponse} pageResponse, {PageRequestStatus} pageRequestStatus) 

Any ideas how to do this?

like image 328
Josh Johnson Avatar asked Jul 03 '10 12:07

Josh Johnson


People also ask

Is JSDoc useful?

Using TypeScript consistently offers a lot of benefits, especially for writing clean code to prevent unnecessary bugs and errors.

How do you write comments in JSDoc?

Create JSDoc commentsPosition the caret before the declaration of the method/function or field to document, type the opening block comment /** , and press Enter . WebStorm generates a JSDoc comment with a list of parameters ( @param ) and return values ( @returns ), where applicable.

What are JSDoc tags?

JSDoc is a markup language used to annotate JavaScript source code files. Using comments containing JSDoc, programmers can add documentation describing the application programming interface of the code they're creating.

Who uses JSDoc?

Who uses jsdoc? 18 companies reportedly use jsdoc in their tech stacks, including MEGA, Clovis, and immmr.


1 Answers

You can document stuff that doesnt exist in the code by using the @name tag.

/**  * Description of the function  * @name IDontReallyExist  * @function  * @param {String} someParameter Description */  /**  * The CallAgain method calls the provided function twice  * @param {IDontReallyExist} func The function to call twice */ exports.CallAgain = function(func) { func(); func(); } 

Here is the @name tag documentation. You might find name paths useful too.

like image 77
Eric Avatar answered Sep 21 '22 19:09

Eric