JSDoc中的文档变量如何是一个包含对象的数组?

时间:2016-02-26 12:29:38

标签: javascript jsdoc

我必须遵循数据结构:

this.subscribers = [
  {
    callback: () => {console.log( 'my callback'); },
    interval: 5,
    remaining: 3
  },
  {
    callback: () => {console.log( 'my callback 2'); },
    interval: 20,
    remaining: 1
  }
];

我已经像这样编写了JSDoc:

/**
 * This variable store the array of callbacks to call repeatedly with the specified interval.
 * @property {function} callback  - The callback function the SchedulerService call.
 * @property {number}   interval  - The number of minutes between the runs.
 * @property {number}   remaining - The remaining minutes until the next call.
 * @type {Array}
 */
this.subscribers = [];

但是使用这个jsdoc,subscriptions变量应该如下所示:

this.subscribers = [];
this.subscribers.callback = () => {};
this.subscribers.interval = 10;
this.subscribers.remaining = 2;

此属性的正确JSDoc注释如何?

注意:我们在这里讨论@property而不是@param

1 个答案:

答案 0 :(得分:1)

{[].<TypeOfObject>}

{Array.<TypeOfObject>}

我发现它有点尴尬,但您可以在docs中看到该行的示例:

数组和对象(类型应用程序和记录类型)

在您的情况下,您首先要定义Subscriber类型:

/**
 * @typedef {Object} Subscriber
 * @property {function} callback  - The callback function the SchedulerService call.
 * @property {number}   interval  - The number of minutes between the runs.
 * @property {number}   remaining - The remaining minutes until the next call.
 */

然后你会在构造函数中引用类型(它是什么):

/**
 * This variable store the array of callbacks to call repeatedly with the specified interval.
 * @type {Array.<Subscriber>}
 */
this.subscribers = [];