【问题标题】:JSDoc Object of templated objects模板化对象的 JSDoc 对象
【发布时间】:2018-06-28 14:03:18
【问题描述】:

有没有办法松散地指定你正在记录的对象中应该包含什么类型的对象?

我正在寻找记录以下对象:

var obj = {
  unknownName1: {
    name: "KnownValue"
  },
  unknownName2: {
    name: "KnownValue",
    offset: {
      x: 0,
      y: 0
    }
  },
  unknownName3: {
    name: "KnownValue",
    offset: {
      x: 0,
      y: 0
    },
    visible: true
  },
  unknownName4: {
    name: "KnownValue"
  }
};

子对象应具有以下属性:

/**
 * Example Object
 * @typedef myObject
 * @type {Object}
 * @property {String} name - Name of the templated object
 * @property {Number} [offset.x] - Offset X
 * @property {Number} [offset.y] - Offset Y
 * @property {Boolean} [visible] - Is Visible
 * @memberof com.namespace.MyClass 
 */

如果我想记录这个特定的obj,我会执行以下操作,但是该对象将动态生成,其中包含未知数量的未知名称对象,类型为com.namespace.MyClass

/**
 * Object of Special Objects
 * @typedef mySpecialObjectOfObjects
 * @type {Object}
 * @property {com.namespace.MyClass.myObject} unknownName1
 * @property {com.namespace.MyClass.myObject} unknownName2
 * @property {com.namespace.MyClass.myObject} unknownName3
 * @property {com.namespace.MyClass.myObject} unknownName4
 * @memberof com.namespace.MyClass
 */

附:我只是在寻找一个可以使用的通配符@property,这样我的编辑器就可以帮助我记住对象内每个子对象的所有可用选项。

【问题讨论】:

    标签: javascript documentation jsdoc


    【解决方案1】:

    根据http://usejsdoc.org/tags-type.html,从 JSDoc 3.2 开始,JSDoc 已经完全支持 Google Closure Compiler 类型表达式。 http://usejsdoc.org/tags-type.html#jsdoc-types 描述了一种这样的格式:

    {Object.<string, number>}
    

    所以在你的情况下,你应该能够做到:

    /**
     * Object of Special Objects
     * @typedef mySpecialObjectOfObjects
     * @type {Object.<string, com.namespace.MyClass.myObject>}
     * @memberof com.namespace.MyClass
     */
    

    你甚至可以用它自己的类型替换string,如果你想有一个专用于名称的特殊类型,详细说明允许的字符串值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-26
      • 2019-05-05
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多