javascript - 如何记录返回特定值的函数?

标签 javascript documentation jsdoc jsdoc3

我正在尝试记录以下仅返回三个值的方法:'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.

如果没有,我应该怎么做?

最佳答案

不幸的是,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 结果:

Result

关于javascript - 如何记录返回特定值的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50107272/

相关文章:

java - 是否可以在右侧而不是顶部创建文档注释?

objective-c - 如何用 doxygen 记录 Objective-C block ?

javascript - VS Code - 如何使用 JSDoc 记录对象

javascript - 可选的 React useState 类型与 jsdoc、typescript checkjs (javascript)

javascript - 如何检测立方体的哪一侧被单击

javascript - 保存 JQuery UI droppables 的顺序

javascript - 基于javascript变量渲染jsp内容

c++ - 氧气警告 : ignoring unknown tag

javascript - 如何在 JSDoc 中指定 promise 的解析和拒绝类型?

javascript - 如何仅在 WooCommerce 结帐页面中启用 Bootstrap css