【问题标题】:How to document an object within a class constructor?如何在类构造函数中记录对象?
【发布时间】:2019-04-28 15:00:37
【问题描述】:

我目前正在使用 JSDoc 来尝试记录以下代码...

class Test {
  /**
   * @param {Object} raw The raw data.
   */
  constructor(raw) {
    /**
     * Used for things and stuff. It can be useful when referencing Test.myObject.myValue.
     * @type {Object}
     */
    this.myObject = {
      /**
       * This is my string... It does things. Very useful.
       * @type {String}
       */
      myValue: raw.thisIsMyValue
    }
  }
}

但我不完全确定该怎么做。上面的示例在 VSCode 中有效,但在生成文档时无效。我尝试了一个 typedef,但这并没有奏效,因为它使它成为一个全局 typedef,而不是成为 Test 类原型的一部分。我该怎么做呢?

我知道如何使用@param 为函数定义“匿名”对象,但我不知道如何为类原型定义。我已经在谷歌上搜索了一个半小时以上,但没有运气。

【问题讨论】:

标签: javascript node.js jsdoc jsdoc3 documentationjs


【解决方案1】:

我想通了

  1. 我的调试方法没有帮助,因为我用于文档生成器的主题隐藏了属性。
  2. 在创建 typedef 时,您在对象之前取出类型,然后将其列为属性!

例子

class Test {
  /**
   * @param {Object} raw The raw data.
   */
  constructor(raw) {
    /**
     * Used for things and stuff. It can be useful when referencing Test.myObject.myValue.
     * @typedef MyObject
     * @property {String} myValue This is my string... It does things. Very useful.
     */
    this.myObject = {
      myValue: raw.thisIsMyValue
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多