skip to Main Content
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;
    }
};

2

Answers


  1. 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.

    Login or Signup to reply.
  2. The @param and @returns tags belongs inside the JSDoc comment for the set method, not in the @typedef comment for ObjectName.

    VSCode works if I remove

        /**
         * An instance of ObjectName.
         * @type {ObjectName}
         */
    

    Here is a corrected version

    /**
     * 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.
     */
    
    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;
      }
    };
    

    VSCode:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search