如何使用 JSDoc 注释记录具有属性和参数的 JavaScript 对象

问题描述 投票:0回答:2
const objectname = {
    property: 0,
    set (value) {
        this.property = value;
    }
}

您将如何使用 JSDoc 注释来记录这些示例行?

这是我已经尝试过的,但它没有显示

objectName.set
作为方法,也没有显示其参数。

/**
 * 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;
    }
};
javascript jsdoc
2个回答
0
投票

我不是 JSDoc 专家,也许我太简单了,但这似乎有效:

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

当我在 VS Code 中使用它时,我会在弹出窗口等中获取事物的类型和描述(包括

set
上的参数类型)。


0
投票

@param 和 @returns 标签属于 set 方法的 JSDoc 注释,而不是 ObjectName 的 @typedef 注释。

这是更正后的版本

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

/**
 * 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;
  }
};
© www.soinside.com 2019 - 2024. All rights reserved.