QgEnum.js
1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* TODO 类型说明 by xqq
*/
class QgEnum {
constructor(data) {
if (!Array.isArray(data)) {
throw Error('argument is not an array!');
}
// 保存源数组
this._source = data;
this.getTypeArr();
}
getTypeArr() {
this.typeArr = [];
this._source.map(element => {
if (!element.key || !element.text) {
return;
}
this.typeArr.push(element.key);
// 根据key生成不同属性值,以便A.B.text类型的调用
Object.defineProperty(this, element.key, {
value: element,
writable: false
});
// this[element.key] = element;
});
// console.log(this);
return this;
}
values() {
return this._source;
}
// 根据key得到对象
valueOf(key) {
let prop = {
key: '',
text: ''
};
if (this.typeArr.indexOf(key) === -1) {
prop = '';
console.error(`${key}字段不存在该枚举数组中`);
} else {
prop = this._source[this.typeArr.indexOf(key)];
}
return prop;
}
// 根据key获取text值
getText(key) {
const prop = this.valueOf(key);
let text = '--';
if (prop) {
text = prop.text;
}
return text;
}
}
export default QgEnum;