【问题标题】:How to document a function that returns specific values?如何记录返回特定值的函数?
【发布时间】:2018-10-10 22:44:55
【问题描述】:

我正在尝试记录以下仅返回三个值的方法:'active'、'inactive' 或 'blocked'。

Javascript 代码:

/**
 * Returns the current post status.
 * @return {string} the current status: 'inactive', 'active' or 'blocked'.
 */
function getStatus() {
    if (this.status === 0) return 'inactive';
    if (this.status === 1) return 'active';
    if (this.status === 2) return 'blocked';
}

有没有办法指定返回值?也许是这样的:

@return {string=('inactive'|'active'|'blocked')} the current status.

如果没有,我该怎么做?

【问题讨论】:

  • 您是否考虑过使用 TypeScript 或 Flow?如果你想要更强的打字,那么这可能是需要考虑的事情。
  • @E.Sundin 感谢您的评论,我只想将 Jsdoc 用于文档目的。
  • @E.Sundin 在 TypeScript 中是否可行?
  • @CryptoBird - 请注意,如果需要,您可以编辑您的 cmets 而不是发布新的。
  • 就我个人而言,我还没有找到此类工具的用途,所以我不知道存在什么。 Here's two typescript examples

标签: javascript documentation jsdoc jsdoc3


【解决方案1】:

不幸的是,JSDoc中没有这样的东西,我能想到的唯一解决方案就是像这样使用@enum标签:

/**
 * Post status.
 * @readonly
 * @enum {string}
 */
var STATUS = {
    /** The post is inactive. */
    INACTIVE: 'inactive',
    /** The post is active. */
    ACTIVE: 'active',
    /** The post is blocked. */
    BLOCKED: 'blocked'
};

/**
 * Returns the current post status.
 * @return {STATUS} the current status.
 */
function getStatus() {
    if (this.status === 0) return STATUS.INACTIVE;
    if (this.status === 1) return STATUS.ACTIVE;
    if (this.status === 2) return STATUS.BLOCKED;
}

JSDoc 结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 2014-11-15
    • 2017-03-16
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多