Search code examples
javascriptdocumentationjsdoc

How to document this using jsdoc?


I'd like to know how to document this type of class using jsdoc:

var MyObject = (function(){

  var that = {};

  function privateFunction(){};

  that.publicFunction = function(){};

  that.publicField = "foo";

  return that;

})();

Solution

  • There are a number of things named JSDoc, but using closure compiler annotations which work with jsdoc toolkit, you can use @constructor to mark MyClass as a constructor.

    /** @constructor */
    var MyClass = ...;
    

    Then you can make it clear that that is of the nominal type MyClass though obviously that nominal type won't work with instanceof.

    /** @type MyClass */
    var that = /** @type {MyClass} */ {};
    

    The first @type establishes the type of the declaration, and the second is a type assertion/cast for the value.

    With the methods you can use the @this annotation.

    /** @this MyClass */
    that.publicFunction = function () { ... };