我有一个接受一个字符串参数的函数。此参数只能有几个已定义的可能值中的一个。记录相同内容的最佳方式是什么?应该将shapeType定义为枚举、TypeDef还是其他什么?
Shape.prototype.create = function (shapeType) {
// shapeType can be "rect", "circle" or "ellipse"...
this.type = shapeType;
};
Shape.prototype.getType = function (shapeType) {
// shapeType can be "rect", "circle" or "ellipse"...
return this.type;
};
问题的第二部分是,在将shapeType
定义为您建议的内容的文件中,shapeType
的可能值是未知的。有几个开发人员贡献了多个文件,他们可能会增加shapeType
的可能值。
PS:我正在使用jsdoc3
发布于 2013-10-12 00:09:51
如何声明一个虚拟枚举:
/**
* Enum string values.
* @enum {string}
*/
Enumeration = {
ONE: "The number one",
TWO: "A second number"
};
/**
* Sample.
* @param {Enumeration} a one of the enumeration values.
*/
Bar.prototype.sample = function(a) {};
b = new Bar();
bar.sample(Enumeration.ONE)
为此,您至少需要向JSDOC声明枚举。但是代码是干净的,并且您可以在WebStorm中自动完成。
然而,多个文件的问题不能以这种方式解决。
发布于 2016-01-12 21:19:53
对于jsdoc3中的late 2014,您可以这样写:
/**
* @param {('rect'|'circle'|'ellipse')} shapeType - The allowed type of the shape
*/
Shape.prototype.getType = function (shapeType) {
return this.type;
};
当然,这不会像专用枚举那样可重用,但在许多情况下,如果虚拟枚举只由一个函数使用,那么它就是一种过度的杀伤力。
另请参阅:https://github.com/jsdoc3/jsdoc/issues/629#issue-31314808
发布于 2018-09-19 16:14:08
下面是什么:
/**
* @typedef {"keyvalue" | "bar" | "timeseries" | "pie" | "table"} MetricFormat
*/
/**
* @param format {MetricFormat}
*/
export function fetchMetric(format) {
return fetch(`/matric}`, format);
}
https://stackoverflow.com/questions/19093935
复制相似问题