views:

85

answers:

3

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). [Answer]

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? [Answer]

A: 

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.

no
Thanks, no. I'm doing that currently (without the ifdef) and it works, but I'd like for the user to be able to see immediately that it's a function that accepts params X and Y without leaving where they're at. Similar to how the google map api does it. example: http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsService
Josh Johnson
Just found out that @link can do what I'm talking about. It's not perfect but it works. I'll create a separate answer in case anyone else finds it useful.
Josh Johnson
A: 

@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.

Josh Johnson
+1  A: 

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 copy twice
        */
        exports.CallAgain = function(func) { }

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

Eric