Best way to document anonymous objects and functions with jsdoc Best way to document anonymous objects and functions with jsdoc javascript javascript

Best way to document anonymous objects and functions with jsdoc


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.


You can use @callback or @typedef.

/** * @callback arrayCallback * @param  {object} element - Value of array element * @param  {number} index   - Index of array element * @param  {Array}  array   - Array itself *//** * @param {arrayCallback} callback - function applied against elements * @return {Array} with elements transformed by callback */Array.prototype.map = function(callback) { ... }


To compliment studgeek's answer, I've provided an example that shows what JsDoc with Google Closure Compiler lets you do.

Note that the documented anonymous types get removed from the generated minified file and the compiler ensures valid objects are passed in (when possible). However, even if you don't use the compiler, it can help the next developer and tools like WebStorm (IntelliJ) understand it and give you code completion.

// This defines an named type that you don't need much besides its name in the code// Look at the definition of Page#getPage which illustrates defining a type inline/**  @typedef { pageId : string, pageName : string, contents: string} */var PageResponse;/** * @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     *     * The type for the second parameter for the function below is defined inline     *     * @param {function(PageResponse, {statusCode: number, statusMsg: string})} callback     *        Function executed when page is retrieved     */    this.getPage = function(pageRequest, callback) {    }; };