首页 > 解决方案 > 如何记录返回特定值的函数?

问题描述

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

如果没有,我该怎么做?

标签: javascriptdocumentationjsdocjsdoc3

解决方案


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

结果


推荐阅读