【发布时间】:2019-01-25 20:15:41
【问题描述】:
是否可以在装饰器中获取“类对象”(构造函数)的句柄?
背景:我想用带有类型标记的字符串值解析 json'ish 格式,例如"@date:2019-01-25" 或 "@latlong:51.507351,-0127758"。
这是对旧 js 库的现代化改造,通过覆盖子类化和实例创建来实现。
装饰器看起来很有前途,至少我可以将标签定义为类属性:
function dkdatatype({tag}) {
return function decorator(cls) {
if (cls.kind !== 'class') throw `not class ${cls.kind}`;
cls.elements.push({
kind: 'field',
key: 'tag',
placement: 'static',
descriptor: {
configurable: false,
enumerable: true,
writable: false
},
initializer: () => tag
});
return {
kind: 'class',
elements: cls.elements
};
};
}
@dkdatatype({tag: '@date:'})
export class DkDate extends datatype {
constructor(...args) {
super();
const clstag = this.constructor.tag;
if (typeof args[0] === 'string' && args[0].startsWith(clstag)) {
this.value = new Date(args[0].substr(clstag.length));
} else {
this.value = new Date(...args);
}
}
toJSON() {
return this.constructor.tag + this.value.toISOString().slice(0, 10);
}
}
我可以手动将类添加到类型注册表中:
type_registry[DkDate.tag] = DkDate
但是有没有办法从装饰器(或者可能是基类或其他方式)自动(并且只有一次)执行此操作?
【问题讨论】:
标签: javascript decorator