Search code examples
javascriptdocumentationjsdocjsdoc3

How to document simple class within a self-invoking function?


How to document following code fragment? When I leave it as it is Foo.bar has no description. When I add @memberof tag it will be documented as static property. Adding @instance tag changes nothing. This is rather simple and common code pattern, so it should be easy to document, am I right? I hate jsdoc...

/**
 * @namespace
 */
var ns;

(function (ns) {
    'use strict';

    /**
     * Constructs class
     * @class ns.Foo
     */
    function Foo() {
    }

    /**
     * Blabla
     */
    Foo.prototype.bar = function () {

    };

    ns.Foo = Foo;

})(ns || (ns = {}));

Solution

  • Use @lends to tell jsdoc that what you have in the function belongs to your namespace. This way you don't have to pepper your code with @memberof everywhere. The following works when I run it here:

    /**
     * @namespace
     */
    var ns;
    
    (/** @lends ns */ function (ns) {
        'use strict';
    
        /**
         * Constructs class
         * @class
         */
        function Foo() {
        }
    
        /**
         * Blabla
         */
        Foo.prototype.bar = function () {
    
        };
    
        ns.Foo = Foo;
    
    })(ns || (ns = {}));