Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to document a JavaScript object with properties and methods with parameters using JSDoc comments

const objectname = {
    property: 0,
    set (value) {
        this.property = value;
    }
}

How would you document these example lines with JSDoc comments?

This is what I already tried but it doesn’t show objectName.set as a method and doesn’t show its parameters.

/**
 * Represents an object with a property and a set method.
 * @typedef {object} ObjectName
 * @property {number} property - The numeric property of the object.
 * @property {Function} set - Sets the property value to the specified value.
 * @param {number} value - The value to set the property to.
 * @returns {void}
 */

/**
 * An instance of ObjectName.
 * @type {ObjectName}
 */
const objectName = {
    property: 0,

    /**
     * Sets the property value to the specified value.
     * @param {number} value - The value to set the property to.
     * @returns {void}
     */
    set(value) {
        this.property = value;
    }
};

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I’m not a JSDoc expert, and maybe I’m being too simplistic, but this seems to work:

/**
 * An object that blah blah blah...
 */
const objectname = {
    /**
     * The numeric property of the object.
     * @type number
     */
    property: 0,
    /**
     * Sets the `property` to the given value.
     * @param {number} value - The value to set the property to.
     * @returns {void}
     */
    set(value) {
        this.property = value;
    },
};

When I use that in VS Code, I get the type and description of things (including the parameter type on set) in popups and such.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading