Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document the properties of the object in the JSDoc 3 tag: @this

Tags:

this

jsdoc

The @param tag allow the documentation of properties, e.g.

 /**
  * @param {Object} userInfo Information about the user.
  * @param {String} userInfo.name The name of the user.
  * @param {String} userInfo.email The email of the user.
  */

How would I document the properties of the @this tag?

 /**
  * @this {Object} 
  * @param {String} this.name The name of the user.
  * @param {String} this.email The email of the user.
  */

I'm wondering if anyone working on the project knows. (The docs are still being created...)

like image 255
Matt Avatar asked May 07 '12 23:05

Matt


People also ask

What is the JSDoc keyword to specify an argument to a function?

The @param tag provides the name, type, and description of a function parameter. The @param tag requires you to specify the name of the parameter you are documenting.

How do you write comments in JSDoc?

JSDoc comments should generally be placed immediately before the code being documented. Each comment must start with a /** sequence in order to be recognized by the JSDoc parser. Comments beginning with /* , /*** , or more than 3 stars will be ignored.

What is Typedef in JSDoc?

The @typedef tag is useful for documenting custom types, particularly if you wish to refer to them repeatedly. These types can then be used within other tags expecting a type, such as @type or @param. Use the @callback tag to document the type of callback functions.

What is JSDoc in Nodejs?

JSDoc is an open source API documentation generator for Javascript. It allows developers to document their code through comments.


3 Answers

To document instance members, use @name Class#member:

/**  * Construct a new component  *  * @class Component  * @classdesc A generic component  *  * @param {Object} options - Options to initialize the component with  * @param {String} options.name - This component's name, sets {@link Component#name}  * @param {Boolean} options.visible - Whether this component is visible, sets {@link Component#visible}  */ function Component(options) {    /**     * Whether this component is visible or not     *     * @name Component#visible     * @type Boolean     * @default false     */     this.visible = options.visible;     /**     * This component's name     *     * @name Component#name     * @type String     * @default "Component"     * @readonly     */     Object.defineProperty(this, 'name', {         value: options.name || 'Component',         writable: false     }); } 

This results in a Members section in the documentation that lists each member, its type, default value, and whether it's read only.

The output as generated by [email protected] looks like this:

JSDoc3 output

See also:

  • JSDoc namepaths
  • @instance tag
  • @readonly tag
  • @type tag
like image 78
lazd Avatar answered Sep 23 '22 02:09

lazd


Use the @property tag to describe the attribute of an object.

@param is used to define the parameters of a method or constructor.

@this is used to define which object this refers to. So here's an example using JSDOC 3.

/** * @class Person * @classdesc A person object that only takes in names. * @property {String} this.name - The name of the Person. * @param {String} name - The name that will be supplied to this.name. * @this Person */ var Person = function( name ){     this.name = name; }; 

JSDOC 3: https://github.com/jsdoc3/jsdoc

More information: http://usejsdoc.org/index.html

More info: http://code.google.com/p/jsdoc-toolkit/wiki/TagParam

like image 39
Larry Battle Avatar answered Sep 24 '22 02:09

Larry Battle


Within the constructor of a class, jsdoc will realize by itself that the documented properties belong to class intances. So this should be enough:

/**
 * @classdesc My little class.
 *
 * @class
 * @memberof module:MyModule
 * @param {*} myParam Constructor parameter.
 */
function MyLittleClass(myParam) {
    /**
     * Instance property.
     * @type {string}
     */
    this.myProp = 'foo';
}

If it is not clear for jsdoc that the function is the class constructor, you can use @this to define what this refers to:

/**
 * @classdesc My little class.
 *
 * @class
 * @memberof module:MyModule
 * @name MyLittleClass
 * @param {*} myParam Constructor parameter.
 */

// Somewhere else, the constructor is defined:
/**
 * @this module:MyModule.MyLittleClass
 */
function(myParam) {
    /**
     * Instance property.
     * @type {string}
     */
    this.myProp = 'foo';
}
like image 24
Ignitor Avatar answered Sep 26 '22 02:09

Ignitor