当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - 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?

网友答案:

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) {
    }; 
};
网友答案:

@link can add inline links to methods and classes.

/**
 * 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<br />
 * function({@link PageResponse} pageResponse,{@link PageRequestStatus} pageRequestStatus)
 */
this.getPage = function (pageRequest, callback) {
};

Not ideal, but it gets the job done.

网友答案:

The Google Closure Compiler Annotations has Type Expressions for this which includes the ability to indicate type for specific arguments, return type, and even this. Many libraries are looking at following the Google Closure Compiler Annotations, because they want to use it to shrink their code. So it's got some momentum. The downside is I don't see a way to give the description.

For providing the description perhaps the JSDoc Toolkit Parameters With Properties approach would work (look at the bottom of the page). It's what I am doing right now. The JSDoc Toolkit is prepping to start work on V3, so feedback there might be good.

网友答案:

You could use @see to link to another method within the same class. The method would never be used, it would just be there for documentation purposes.

/**
 * @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
     * @see #getPageCallback 
     */
    this.getPage = function (pageRequest, callback) {
    }; 

    /**
     * Called when page request completes 
     * @param {PageResponse} pageResponse The requested page
     * @param {PageRequestStatus} pageRequestStatus Status of the page
     */
    //#ifdef 0
    this.getPageCallback = function (pageResponse, pageRequestStatus) { };
    //#endif 
};

If you are using some kind of build system, the dummy method could easily be omitted from the build.

分享给朋友:
您可能感兴趣的文章:
随机阅读: