javascript - 如何记录返回特定值的函数?
问题描述
我正在尝试记录以下仅返回三个值的方法: '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 结果:
推荐阅读
- excel - 向文件夹中的所有工作簿添加新的和删除旧的 VBA
- git - “git grep
" 仅适用于 python(或任何类型的)文件? - html - CSS ul li 格式化
- python - 激活 python 虚拟环境后,输入 `python` 仍然会得到 PATH 中的版本
- c# - 发送电子邮件后,消息 ID (PR_INTERNET_MESSAGE_ID_W_TAG) 在事件处理程序中不可用
- arrays - Google 表格脚本帮助 - 存储/写入表格 ID、表格名称、组合范围的单元格位置
- python - 我正在尝试在 notepad++ 中设置 python,但它无法理解其中有空格的文件路径
- sql - 回顾 N 个月并将它们添加为列 spark sql 聚合
- python - 最大月值,同时保留出现该值的数据
- android - 关于 SMS Retriever API 的哈希键