How to document anonymous functions (closure) with jsdoc-toolkit

前端 未结 2 1727
遥遥无期
遥遥无期 2020-12-09 01:27

I am trying to document my code using JSDoc-toolkit. My code starts by being wrapped with a self-executing anonymous function. How in the world do I document this? I\'ve spe

相关标签:
2条回答
  • 2020-12-09 02:20

    You can use @namespace with @name and @lends like this:

    /**
    * @name MyNamespace
    * @namespace Hold all functionality
    */
    (function () {
        "use strict";
        /** @lends MyNamespace*/
        var stlib = function (param, param, param) { ...All of my code...};
    }());
    
    0 讨论(0)
  • 2020-12-09 02:22

    You can't document nested functions directly. But you can do something like this:

    /**
     * @module foobar
     */
    
    /**
    * @function
    * @author Baa
    * @name hello 
    * @description Output a greeting
    * @param {String} name - The name of the person to say hello
    */
    (function hello(name) {
        /**
         * @function
         * @author Baz
         * @inner
         * @private
         * @memberof module:foobar
         * @description Check if the argument is a string (see: {@link module:foobar~hello})
         * @param {String} string - The string
         * @returns {String} Returns true if string is valid, false otherwise
         */ 
        var isString = function checkString(string) { return typeof string === 'string'; };
        if (isString(name))
          console.log('Hello ' + name + '!');
    }('Mr. Bubbles'));
    

    Here I'm setting checkString as private and inner to be descriptive (since nested functions can't be described), And then I pass in -p to document private functions. Finally, I add a link to the parent function for reference.

    I think jsdoc is unnecessarily finicky and needs to be replaced with something better. It's a port of javadoc, so it has a lot of things that are relevant to Java but not JS, and vice versa. There are very common JS idioms, like closures or nested functions, that are hard or impossible to document.

    I always check my namepaths and debug using the --explain flag.

    0 讨论(0)
提交回复
热议问题