Search code examples
javascriptannotationsdocumentation-generationjsdoc

JSDoc: Annotating an exported method


If I define an object like this:

/**
 * My Cool Object
 * @constructor
 */

 function MyCoolObject() {

       /**
        * Cool Method, private
        * @param {!string} parameter
        */

       function localMethod(parameter) {
           // do stuff
       }

       // Export the method
       this.exportedMethod = localMethod;

 }

I'd like to know, if at all possible, how to tell JSDOC to use the annotation for localMethod in exportedMethod, or how can I annotate exportedMethod, because if I do:

       // Export the method

       /**
        * Cool Method 
        * @param {!string} parameter
        */
       this.exportedMethod = localMethod;

JSDOC assumes it's a field rather than a method, then only uses the description, ignoring the @param part.


Solution

  • I would reduce it to:

    /**
     * My Cool Object
     * @constructor
     */
    
    function MyCoolObject()  {
        /**
         * Cool Method, private
         * @param {!string} parameter
         */
        this.exportedMethod = function (parameter) {
            // do stuff
        };
    
    }
    

    You can do var localMethod = this.exportedMethod right after if you want a local reference to the method. In the off chance that you've over-simplified your example and you need to first assign to localMethod before assigning to this.exportedMethod you could do this:

    /**
     * My Cool Object
     * @constructor
     */
    
    function MyCoolObject()  {
    
        function localMethod(parameter) {
            // do stuff
        }
    
        /**
         * Cool Method, private
         * @param {!string} parameter
         * @function
         */
        // Export the method
        this.exportedMethod = localMethod;
    
    }
    

    The @function declaration tells jsdoc that it is dealing with a function.